visit
This project contains 2 parts:
In this post, I will show you the first part to build your Server API.
Here is the final code:$ mkdir project && mkdir project/server && mkdir project/client && cd server/
Now, you’re in the Server folder. Create a file package.json and add the below code.
$ touch package.json
{
"name": "server",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node -r esm app.js",
"dev": "nodemon -r esm app.js"
},
}
1. ESLint Airbnb
Install to allow all developers to have the same coding style and follow a right Javascript coding style$ npx install-peerdeps — dev eslint-config-airbnb
Create .eslintrc in your project and add this:
{
"extends": "airbnb"
}
2. Babel
Install : This compiles ES6 to ES5 to compress the project size when pushing to production to reduce run-time and because many web browsers can only read ES5.$ npm install esm babel-cli babel-register babel-preset-es2015 babel-preset-stage-2 babel-core babel-polyfill — save-dev
Create .babelrc file in your server project and add this:
{
"presets": ["es2015", "stage-2"]
}
3. Express Middleware
Install the first 3 middlewares to run your App:$ npm i — save express esm nodemon
Express: an essential framework for NodeJS to start a server project.
: This goes with `babel` and allows you to run ES6.
: This is my favorite; it will enable you to restart the server automatically whenever you make changes in the server.
$ touch app.js
This app.js will start a server on PORT 8080 and initialize all the dependencies that your app requires. Add this simple code to app.js
// Import all dependencies & middleware here
import express from 'express';
// Init an Express App. This later starts a server and put all dependencies into your project to use
const app = express();
// Use your dependencies here
// use all controllers(APIs) here
app.get('/', (req, res) => {
res.status(200).json({
status: 'success'
});
});
// Start Anything here
app.listen(8080, () => {
console.log('Example app listening on port 8080!');
});
You can find the script that runs these functions in package.json
$ npm start
OR (To run automatically whenever you make a new change, run used by nodemon)
$ npm run dev
$ npm i body-parser
Import this to app.js:
import bodyParser from 'body-parser';
// Use your dependencies here
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
Now, your project is getting complicated, and you don’t want to put all your API into app.js, which is used only for starting & initializing your app.
You want to separate your APIs into different folders. Run the following commands:$ mkdir controller
$ touch controller/index.js && touch controller/user.controller.js
Open your user.controller.js and import this code in there:
import express from 'express';
const userController = express.Router();
userController.get('/', (req, res) => {
res.status(200).json({
status: 'success'
});
});
export default userController;
Assume your project has many controllers. You don’t want to keep importing all controllers to your app.js. Then, you want to use 1 file to import all controllers.
Open index.js in your controller and import this:
import userController from './user.controller';
//import abcController from './abc.controller';
//import xyzController from './xyz.controller';
export {
userController,
//abcController,
//xyzController
};
NOTE: the comments are just examples.
You just created the Controller, but you haven’t told Express App to use it.
In app.js, first import Controllers:
import {
userController,
} from './controller';
app.get('/', (req, res) => {
res.status(200).json({
status: 'success'
});
});
app.use('/', userController);
// Uncomment and modify the route if you want to use any controllers
//app.use('/abc', abcController);
//app.use('/xyz', xyzController);
Install & Start MongoDB
You will need to install : “Mongoose provides a straight-forward, schema-based solution to model your application data.”Open a new terminal:
$ brew update
$ brew tap mongodb/brew
$ brew install [email protected]
$ npm i mongoose
In your app.js, import mongoose:
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
And connect MongoDB in app.listen():
app.listen(8080, () => {
console.log(`Started successfully server at port ${port}`);
mongoose.connect('mongodb://localhost/test').then(() => {
console.log(`Conneted to mongoDB at port 27017`);
});
});
Schema
$ mkdir database && mkdir database/models && mkdir database/schemas
$ touch database/schemas/user.schema.js
$ npm i sha256
For example, the User schema will have two attributes: email & hashedPassword.
Open user.schema.js:
import { Schema } from 'mongoose';
import sha256 from 'sha256';
const userSchema = new Schema({
hashedPassword: { type: String, required: true },
email: { type: String, required: true },
});
/**
* @param {*} password
*/
userSchema.methods.comparePassword = function comparePassword(password) {
return this.hashedPassword === sha256(password);
};
export default userSchema
Models
Then, you want to create a model for that each schema you create and add them into index.js (so you only need to call one file):
$ touch database/models/user.model.js
Open user.model.js:
import mongoose from 'mongoose';
import userSchema from '../schemas/user.schema';
const User = mongoose.model('User', userSchema);
export default User;
Open models/index.js:
import User from './user.model';
export {
User,
};
Open controller/user.controller.js.
Import User & replace userController.get(‘/’, …) with these 2 new APIs(Endpoints):
import {
User
} from '../database/models';
import sha256 from 'sha256';
/**
* GET/
* retrieve and display all Users in the User Model
*/
userController.get('/', (req, res) => {
User.find({}, (err, result) => {
res.status(200).json({
data: result,
})
})
});
/**
* POST/
* Add a new User to your database
*/
userController.post('/add-user', (req, res) => {
const { email, password } = req.body;
const userData = {
email,
hashedPassword: sha256(password)
}
const newUser = new User(data);
newUser
.save()
.then(data => {
res.status(200).send(data);
})
.catch(err => {
res.status(400).send("unable to save to database");
});
});
Start Database:
$ mongod -config /usr/local/etc/mongod.conf
Start server
$ npm start
Use POST/ method and enter localhost:8080/add-user. This will call the “/add-user” API.
Add this to your body (Postman), If you don't know where to put this. Please watch the video first.{
'email': '[email protected]',
'password': '123456789'
}
Check if your user data is saved to the database, open Web Browser and enter localhost:8080/
Now, you’re done! Congratulations on building your first API.project
├── client
└── server
└── controller - Storing APIs of the app (GET, POST, PUT, DELETE)
├── index.js
└── user.controller.js
└── database
├── model - store all the models of the project
└──schema - create attribute for each model
├── global.js - storing your configuration attribute
├── .eslintrc - config ESLint Airbnb Coding Style
├── .babelrc - migrate ES6 -> ES5 to run on different browsers
├── package.json - config ESLint Airbnb Coding Style
└── App.js - Everything a server needs to start