-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
30 lines (22 loc) · 798 Bytes
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// set up express app
const app = express();
// connect to DB
mongoose.connect('mongodb://localhost/computerstore', {useNewUrlParser: true, useFindAndModify: false});
mongoose.Promise = global.Promise;
const db = mongoose.connection;
app.use(bodyParser.json());
// initialize routes
app.use('/api', require('./routes/api'));
// error handling middleware
app.use(function (err, req, res, next) {
console.log(err);
res.status(404).json({status: 404, error: "no product of this id"});
});
app.get('*', function (req, res) {
res.json({status: 404, msg: "page not found, please refer to /api/products"});
});
app.listen(3000);
console.log("listening to port 3000");