-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
45 lines (32 loc) · 1.07 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const express = require('express');
const bodyParser = require('body-parser');
const validateData = require('./middlewares/dataValidator');
const { body } = require('express-validator');
const {createPerson, readPerson} = require('./controller/controller');
const routes = require('./routes/router');
const dotenv = require('dotenv');
const { connectDB } = require('./database/connection');
const app = express();
dotenv.config();
connectDB();
PORT = process.env.PORT || 8081;
// define validation rules
const postValidationRules = [
// validate that name field is non empty
body('name').isString().notEmpty(),
]
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// test route
app.get('/', (req, res) => {
res.send('API server is running...');
});
// ping route
app.get('/ping', (req, res) => {
res.send('Pong!!, still running...');
});
// CREATE ROUTE
app.post('/api', postValidationRules, validateData, createPerson);
// READ, UPDATE AND DELETE ROUTES
app.use('/api', routes);
app.listen(PORT, console.log(`server is running on port ${PORT}`));