-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
50 lines (39 loc) · 1.27 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Import express
var express = require('express');
var app = module.exports = express();
var bodyParser = require('body-parser');
var allowCors = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '127.0.0.1:5000');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Credentials', 'true');
next();
}
app.listen(5000);
app.use(allowCors);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Import Mongoose
let mongoose = require('mongoose');
// Import routes
let apiRoutes = require("./api-routes")
// Configure bodyparser to handle post requests
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Connect to Mongoose and set connection variable
mongoose.connect('mongodb://127.0.0.1/starwars-restfull', { useNewUrlParser: true });
var db = mongoose.connection;
// Setup server port
var port = 8080;
// Send message for default URL
app.get('/', (req, res) => res.send('Hello World with Express'));
// Use Api routes in the App
app.use('/api', apiRoutes)
// Launch app to listen to specified port
app.listen(port, function () {
console.log("Running RestHub on port " + port);
});