Nodstarter is a starter application for Node.js designed to facilitate rapid RESTful API development. It provides pre-configured, secured routers, services, and models to streamline the creation of secure and scalable applications.
- General Info
- Technologies
- Initial Code
- Project Structure
- Code Example
- Sign In API Call Example
- Environment Variables
- Setup
- Before Running
- Demo
- New Ideas
- Future Plans
- Contributing
- License Summary
- NPM Statistics
- Project Based
Nodstarter provides a framework for developing secure RESTful APIs quickly. It includes pre-configured routers, services, and models, allowing developers to focus on writing business logic rather than setup and configuration.
- Node.js: JavaScript runtime for building scalable network applications.
- Express: Web framework for Node.js.
- MongoDB: NoSQL database for storing data.
- Mongoose: ORM for MongoDB.
- Lodash: Utility library for JavaScript.
- Morgan: HTTP request logger middleware.
- Body-parser: Middleware for parsing request bodies.
- Method-override: Middleware to allow HTTP verbs such as PUT or DELETE.
- Bcrypt: Library for hashing passwords.
- Express-jwt: Middleware for JWT authentication.
- Jsonwebtoken: Library for signing and verifying JSON Web Tokens.
- Cors: Middleware for enabling Cross-Origin Resource Sharing.
- Nodemon: Utility for auto-reloading the server during development.
- Testing Libraries: Supertest, Chai, and Mocha for testing.
Nodstarter includes models for users
, posts
, and categories
, with predefined relationships:
- User to Posts: One-to-many.
- Posts to Categories: Many-to-many.
These components are organized into routers, controllers, and models to provide a clear structure for API development.
├── index.js
├── package.json
├── scripts
│ └── post_install.js
└── server
├── api
│ ├── categories
│ │ ├── controller.js
│ │ ├── model.js
│ │ └── router.js
│ ├── index.js
│ ├── posts
│ │ ├── controller.js
│ │ ├── model.js
│ │ └── router.js
│ └── users
│ ├── controller.js
│ ├── model.js
│ └── router.js
├── auth
│ ├── controller.js
│ ├── index.js
│ └── routers.js
├── config
│ ├── development.js
│ ├── index.js
│ ├── production.js
│ └── testing.js
├── index.js
├── middleware
│ ├── err.js
│ └── index.js
└── util
├── createRouter.js
└── logger.js
This is the configuration to the JWT token to the user
const expressJwt = require('express-jwt');
const config = require('../config');
const logger = require('../util/logger');
const jwt = require('jsonwebtoken');
const checkToken = expressJwt({
secret: config.secrets.jwt
});
const User = require('../api/users/model');
exports.decodeToken = () => {
return (req, res, next) => {
if (req.query && req.query.hasOwnProperty('access_token')) {
req.headers.authorization = 'Bearer ' + req.query.access_token;
}
checkToken(req, res, next);
};
};
exports.getFreshUser = () => {
return (req, res, next) => {
User.findById(req.user._id)
.then((user) => {
if (!user) {
res.status(400).send('Unanuthorized');
} else {
req.user = user;
next();
}
}, (err) => {
next(err);
});
};
};
exports.verifyUser = () => {
return (req, res, next) => {
var username = req.body.username;
var password = req.body.password;
if (!username || !password) {
res.status(400).send('You need a username and password');
return;
}
User.findOne({
username: username
})
.then((user) => {
logger.log('the selected user from DB: ' + user);
if (!user) {
logger.log('No user with the given username');
res.status(401).send('Invalid username or password');
} else if (!user.authenticate(password)) {
logger.log('Invalid password');
res.status(401).send('Invalid username or password');
} else {
req.user = user;
next();
}
}, (err) => {
next(err);
});
};
};
exports.signToken = (id) => {
logger.log("id is: " + id);
return jwt.sign({
_id: id
}, config.secrets.jwt, {
expiresIn: config.expireTime
});
};
curl --header "Content-Type: application/json" --request POST --data '{"username":"test_user_4","password":"12345"}' http://localhost:3000/auth/signin
, Output
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZTY2NjQwODYzMGE0NDE3MThiMjNhMzgiLCJpYXQiOjE1ODQ2Mzc4NDIsImV4cCI6MTU4NDY1MjI0Mn0.MODWP86ebc8XOMjDGyuvNCWWoKnQhpZpl81ynFGExG8"}
Nodstarter starts searching first for the following environment variables if found them will use them if not will use the default. The default values for NODE_ENV
is development, NODE_PORT
is 3000
and JWT
is Gambell
You can choose the environment for the NODE_ENV
variable from one of the following profiles (development
, production
, testing
)
NODE_ENV
NODE_PORT
JWT
- Init with npm:
$ npm init
- Install:
$ npm install nodstarter
- Start:
$ npm start
, or use in one-line command:
$ npm init; npm install nodstarter; npm start
Please make sure that the MongoDB daemon
is up and running
- Ubuntu
systemctl start mongodb
- Macos
brew services run mongodb-community
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
- Swagger file for the existing APIs
- Add tests for the existing APIs
- Ability to make nodstarter a command line to install models, controllers and routers, etc.
If you have new ideas about the project please describe what you want to do and changes using an issue.
See CONTRIBUTING.md
See License