-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
33 lines (26 loc) · 986 Bytes
/
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
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require("cors");
const app = express();
const routes = require('./src/controller/article.controller');
const config = require('./src/config/db.config');
mongoose.connect(config.url);
const database = mongoose.connection;
database.on('error', (error) => {
console.log(error)
});
database.once('connected', () => {
console.log('Connected successfully to database');
});
app.use(express.json());
app.use(bodyParser.json()); // using bodyParser to parse JSON bodies into JS objects
app.use(express.urlencoded({ extended: true })); // parse requests of content-type - application/x-www-form-urlencoded
app.use(cors());
app.use('/api', routes);
app.get('/', (req, res) => {
res.json({"message": "Welcome to Node Express Articles Rest API"});
});
app.listen(config.port, () => {
console.log(`Server is running on port ${config.port}.`);
});