-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (59 loc) · 2.28 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'use strict';
import mongoose from 'mongoose';
import express from 'express';
import bodyParser from 'body-parser';
import * as model from './api/models/dbcardsModel';
let cards = require('./api/routes/dbcardsRoutes'),
app = express(),
port = process.env.PORT || 3000;
app.set('port', port);
// Mongoose connect.
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://sanlu:#[email protected]:41232/dbzcards');
// Set static folder (frontend main (index) page)
// import path from 'path';
// app.use(express.static(path.join(__dirname, 'src')));
// Body Parser middleware.
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true, parameterLimit: 50000}));
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:4200');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers type you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
// Request headers max age.
res.setHeader('Access-Control-Max-Age', '86400'); // 24h.
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', false);
if (req.method === 'OPTIONS') {
let headers = {};
res.writeHead(200, headers);
res.end();
} else {
// Pass to next layer of middleware
next();
}
});
// Routing: home
app.get('/', (req, res) => {
res.send('Welcome to Awesome Sanlu server API main page.');
});
// Routing: Cards
cards(app);
// Routing: 404.
app.use((req, res) => {
res.status(404).send({url: `${req.originalUrl} not found`});
});
// Listen
app.listen(app.get('port'), () => {
console.log(`Awesome Sanlu server started on: ${port}`);
});
// Notes:
// To start mongodb service: net start mongodb
// To open mongod (if service not working): mongod --config="c:\Program Files\MongoDB\Server\3.4\mongod.cfg"
// To stop mongodb service: net stop mongodb
// To start nodemon service: npm run nodemon-start