https://github.com/agm-dev/noswbi
Node server with batteries included!
https://github.com/agm-dev/noswbi
Last synced: 12 months ago
JSON representation
Node server with batteries included!
- Host: GitHub
- URL: https://github.com/agm-dev/noswbi
- Owner: agm-dev
- License: mit
- Created: 2019-09-16T18:42:57.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-11-04T07:49:52.000Z (over 6 years ago)
- Last Synced: 2025-06-09T20:09:57.247Z (about 1 year ago)
- Language: JavaScript
- Size: 250 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# noswbi
[](https://travis-ci.org/agm-dev/noswbi)
[](https://www.codacy.com/manual/agm-dev/noswbi?utm_source=github.com&utm_medium=referral&utm_content=agm-dev/noswbi&utm_campaign=Badge_Grade)
[](https://www.codacy.com/app/codacy/node-codacy-coverage)



Noswbi is a NodeJS HTTP server with everything but routes configured.
## Install
```bash
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 with `createServer(router, { forceJsonResponse: false })`)
- attach router routes to `/` unless `routesPrefix` 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
```javascript
// Import the two required noswbi methods to create the server:
const { createRouter, createServer } = require("noswbi");
// Create the router:
const router = createRouter();
// Add your logic to the router:
router.get("/status", (req, res) => res.status(200).json({ status: "OK" }));
// Create the server by providing the configured router:
const server = createServer(router);
// Start
server.listen(3000);
```
### Server with social login and protected routes
```javascript
const mongoose = require("mongoose");
const { createRouter, createServer } = require("noswbi");
const router = createRouter();
router.get("/", (req, res) => res.send("holi"));
/**
* The requests to the routes attached to this router require
* an Authorization header with value `JWT ${token}`
*/
const protectedRoutes = createRouter({ requireAuth: true });
protectedRoutes.get("/protected-by-default", (req, res) =>
res.json({
message: "this should be protected by default",
user: req.user
})
);
const options = {
allowCors: true,
forceJsonResponse: true, // default value
domain: process.env.DOMAIN,
routesPrefix: "/", // default value
auth: {
jwtSecret: process.env.JWT_SECRET,
issuer: process.env.JWT_ISSUER,
audience: process.env.JWT_AUDIENCE,
google: { // you will need to create an configure a Google app for login
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_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: require("./path/to/your/user/model") // optional
}
};
const server = createServer([router, protectedRoutes], options);
mongoose.connect(
"mongodb://localhost:27017/test",
{
useNewUrlParser: true,
useUnifiedTopology: true
},
err => {
if (err) {
console.error(err);
process.exit(1);
}
server.listen(3000, () => console.log("running on 3000"));
}
);
```
## Examples
You can check full examples [here](./examples).