-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
48 lines (41 loc) · 1.22 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
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var cors = require('cors');
// CORS setup
var corsOptions = {
origin: 'http://127.0.0.1:9000'
};
// Modules for API resource routes
var login = require('./routes/login/index');
var routes = require('./routes/index');
var recipes = require('./routes/recipes/index');
var tags = require('./routes/tags/index');
// Create express app instance and set middleware
var app = express();
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
// Mount the route modules
app.use('/', routes);
app.use('/login', login);
app.use('/recipes', recipes);
app.use('/tags', tags);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
app.use(function (err, req, res, next) {
var status = err.status || 500;
if (status === 404) {
res.status(status).json({ message: 'We were unable to locate that resource' });
}
else {
res.status(status).json({ message: 'There was an error on the server' });
}
});
module.exports = app;