API Reference v9.3.4

Complete documentation of all methods and properties available in Novaxjs2 version 9.3.4.

Core Methods

MethodDescriptionExample
get(path, ...handlers)Define GET route with optional middlewareapp.get('/', authMiddleware, (req, res) => {...})
post(path, ...handlers)Define POST route with optional middlewareapp.post('/users', validateUser, (req, res) => {...})
put(path, ...handlers)Define PUT route with optional middlewareapp.put('/users/:id', authMiddleware, (req, res) => {...})
delete(path, ...handlers)Define DELETE route with optional middlewareapp.delete('/users/:id', authMiddleware, (req, res) => {...})
group(prefix, callback)Group routes under a common prefixapp.group('/api', ({ get, post }) => {
  get('/users', handler);
  post('/users', handler);
})
at(port, host, callback)Start server on specified portapp.at(3000, 'localhost', () => {...})
on(status, handler)Custom error handler for specific status codesapp.on(404, () => 'Not found')
init(options)Initialize app with configuration optionsapp.init({ staticPath: 'public', minifier: true })
useRouter(router)Use a router module or functionapp.useRouter(userRouter);
app.useRouter(require('./routes/api'));

Middleware Methods

MethodDescriptionExample
useMiddleware(fn)Add standard middlewareapp.useMiddleware((req, res, next) => {...})
useErrorMiddleware(fn)Add error middlewareapp.useErrorMiddleware((err, req, res, next) => {...})
serveStatic(dir)Serve static files from directoryapp.serveStatic('public')

Configuration Methods

MethodDescriptionExample
cors(options)Configure CORS settingsapp.cors({ origins: ['*'] })
setFileSizeLimit(mb)Set file upload size limitapp.setFileSizeLimit(50)
setFileConfig(config)Configure multiple file upload settingsapp.setFileConfig({ maxSize: 10, allowedTypes: ['image/*'] })
setAllowedFileTypes(types)Set allowed MIME types for uploadsapp.setAllowedFileTypes(['image/jpeg', 'image/png'])
setMaxFiles(max)Set maximum number of files per uploadapp.setMaxFiles(5)
setKeepOriginalName(keep)Set whether to keep original filenamesapp.setKeepOriginalName(true)
style(css)Set global CSS stylesapp.style = 'body { color: red }'
js(script)Set global JavaScriptapp.js = 'console.log("Hello")'

Request Object

PropertyDescriptionType
req.methodHTTP method (GET, POST, etc.)String
req.urlRequest URLString
req.queryParsed query parametersObject
req.paramsRoute parametersObject
req.bodyParsed request bodyObject
req.filesUploaded filesObject
req.protocolRequest protocol (http/https)String
req.fullUrlComplete request URLString
req.ipClient IP addressString
req.headersRequest headersObject
req.cookiesParsed cookiesObject

Response Methods

MethodDescriptionExample
res.status(code)Set HTTP status coderes.status(404)
res.json(data)Send JSON responseres.json({ success: true })
res.redirect(url)Redirect to URLres.redirect('/home')
res.send(content, contentType)Send raw content with optional content typeres.send('Hello World', 'text/plain')
res.set(headers)Set multiple headersres.set({ 'X-Custom': 'value' })
res.cookie(name, value, options)Set cookieres.cookie('token', 'abc123', { maxAge: 900000 })
res.clearCookie(name, options)Clear cookieres.clearCookie('token')
res.sendFile(filePath, contentTypeOrRes, res)Send file to clientapp.sendFile('/path/to/file.html', 'text/html', res)

Plugin System

MethodDescriptionExample
usePlugin(plugin, options)Extend framework functionality with pluginsapp.usePlugin(myPlugin, { config: true })

View Engine Methods

MethodDescriptionExample
setViewEngine(engine, options)Configure template engineapp.setViewEngine('novax', { viewsPath: './views' })
render(file, data)Render template with dataawait app.render('home', { title: 'Home' })
addHelper(name, fn)Add template helper functionapp.addHelper('formatDate', (date) => {...})
addHelpers(helpers)Add multiple template helpersapp.addHelpers({ helper1: fn1, helper2: fn2 })

Content Minification Methods

MethodDescriptionExample
minifyContent(content)Minify HTML contentapp.minifyContent(htmlString)
minifyCSS(css)Minify CSS contentapp.minifyCSS(cssString)
minifyJs(js)Minify JavaScript contentapp.minifyJs(jsString)

New in v9.3.4

FeatureDescriptionExample
req.cookiesAutomatic cookie parsingconst token = req.cookies.token;
res.cookie()Set cookies with optionsres.cookie('user', 'john', { httpOnly: true });
res.clearCookie()Clear cookiesres.clearCookie('session');
Enhanced TemplatingNew @ syntax for templates@if(user) Welcome @user.name! @end
Improved Error HandlingBetter error messages and stack tracesapp.on(500, (err) => `Error: ${err.message}`);