Read on my blog.
This article will discuss an example of making a REST API using Express.js, Mongoose and Babel. The resource or endpoint created is only users. We will only make a simple user registration method. We will use a repository pattern.
- Install the package
yarn add express mongoose mongod
yarn add --dev @babel/cli @babel/core @babel/node @babel/preset-env nodemon
- Create folder structure
.
├── package.json
├── src
│ ├── controllers
│ │ └── UserController.js
│ ├── index.js
│ ├── models
│ │ ├── repositories
│ │ │ └── UserRepository.js
│ │ └── User.js
│ └── routes
│ ├── index.js
│ └── users.js
└── yarn.lock
5 directories, 8 files
- User Model
User.js
import mongoose from 'mongoose';
const { Schema } = mongoose;
const schema = new Schema({
email: {
type: String
},
password: {
type: String
}
});
const User = mongoose.model('User', schema);
export default User;
- User Repository
UserRepository.js
Create a repositories
folder in the models folder, then create UserRepository.js
import User from '../User';
class UserRepository {
constructor(model) {
this.model = model;
}
create(object) {
return this.model.create(object);
}
}
export default new UserRepository(User);
Then create UserController.js
in the controllers
folder.
- User Controller
UserController.js
import UserRepository from '../models/repositories/UserRepository';
function createUser(req, res) {
const user = req.body;
UserRepository.create(user)
.then((newUser) => {
res.json(newUser);
}).catch((errors) => {
res.status(500).json({
errors,
});
});
}
export default { createUser };
After that we list the routes in the routes
folder as users.js
- User Route
users.js
import express from 'express';
import UserController from '../controllers/UserController';
const router = express.Router();
router.post('/', UserController.createUser);
export default router;
And index.js on routes
import express from 'express';
import users from './users';
const router = express.Router();
router.use(express.json());
router.use('/users', users);
export default router;
Finally we create index.js
in the src
- Server
index.js
import express from 'express';
import mongoose from 'mongoose';
import routes from './routes';
mongoose.Promise = global.Promise;
const app = express();
app.use('/', routes);
mongoose.connect('mongodb://localhost:27017/db')
.then(() => {
console.log('mongodb started.');
app.listen(8000, () => {
console.log('Server started on 8000');
});
}).catch(() => {
console.log('Mongodb connection failed.');
})
- Command to run the server
nodemon -w src -x "babel-node src --presets @babel/env"
We can add it to npm-scripts
in package.json
{
"name": "exercise",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"express": "^4.17.1",
"mongodb": "^3.2.7",
"mongoose": "^5.6.2",
"nodemon": "^1.19.1"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
"@babel/core": "^7.4.5",
"@babel/node": "^7.4.5",
"@babel/preset-env": "^7.4.5"
},
"scripts": {
"start": "nodemon -w src -x \"babel-node src --presets @babel/env\""
}
}
That was practicing express.js, mongoose and babel using a simple REST API. The entire code can be seen at https://github.com/yoiso/exercise-express
Top comments (0)