noswbi
Noswbi is a NodeJS HTTP server with everything but routes configured.
Install
npm install --save noswbi
Features
Noswbi includes the following configuration / features:
- helmet
- compression
- overload protection (only on production environment)
- body-parser for JSON and urlencoded (extended false)
- cors (can be enabled with `createServer(router, { allowCors: true }))
- forces
Content-Type: application/json
on response headers (can be disabled withcreateServer(router, { forceJsonResponse: false })
) - attach router routes to
/
unlessroutesPrefix
provided on config when creating the server - authentication protection for routes
- social login with Google
- 404 not found handler included
- error handler included
Production
Don't forget to set the NODE_ENV
variable to "production"
. It will improve the performance of the server and will remove the stack trace from error handler. It will also enable the overload protection.
Usage
The idea behind noswbi is to provide a configured server, so you can focus only on coding the routes and logic of your API.
Noswbi exposes two methods: createRouter
and createServer
. You can use them to easily create a server.
Simple server
// Import the two required noswbi methods to create the server:const createRouter createServer = ; // Create the router:const router = ; // Add your logic to the router:router; // Create the server by providing the configured router:const server = ; // Startserver;
Server with social login and protected routes
const mongoose = ;const createRouter createServer = ; const router = ; router; /** * The requests to the routes attached to this router require * an Authorization header with value `JWT ${token}` */const protectedRoutes = ; protectedRoutes; const options = allowCors: true forceJsonResponse: true // default value domain: processenvDOMAIN routesPrefix: "/" // default value auth: jwtSecret: processenvJWT_SECRET issuer: processenvJWT_ISSUER audience: processenvJWT_AUDIENCE google: // you will need to create an configure a Google app for login clientID: processenvGOOGLE_CLIENT_ID clientSecret: processenvGOOGLE_SECRET /** * If you provide an user model with a method findOrCreate * noswbi will call it with the user info provided by Google * after succesful login. */ userModel: // optional ; const server = ; mongoose;
Examples
You can check full examples here.