-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
38 lines (29 loc) · 954 Bytes
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// set up express app
const app = express();
// connect to mongodb
mongoose.connect('mongodb://localhost/studentdb');
mongoose.Promise = global.Promise;
// json
app.use(bodyParser.json());
app.use(express.static('public'));
// CORS fix
app.use((req, res, next) => {
res.header('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers, X-Requested-With');
res.header('Access-Control-Allow-Methods', ' GET, POST, OPTIONS, PUT, DELETE');
res.header('Access-Control-Allow-Origin', '*');
next();
});
// routes
app.use('/api', require('./api'));
// error handling middleware
app.use(function(err, req, res, next){
console.log(err);
res.status(422).send({error: err.message});
});
// listen for requests
app.listen(process.env.port || 4000, function(){
console.log('listening!');
});