-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
49 lines (42 loc) · 1.51 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
'use strict'
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
//const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const config = require('./config');
const tool = require('./tool');
const apiRouter = require('./routes/api');
const app = express();
// babel 编译
require('babel-core/register');
// view engine setup
//app.set('views', path.join(__dirname, 'views'));
//app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// 跨域支持
app.all('/api/*', (req, res, next) => {
const origin = req.headers.origin;
if (config.whiteOrigins.indexOf(origin) !== -1) {
res.header('Access-Control-Allow-Origin', origin);
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, token');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS, DELETE');
}
next();
});
// api
app.use('/api', apiRouter);
// catch 404 and forward to error handler
app.use((req, res, next) => {
tool.l('this is an error')
res.sendFile(path.dirname(require.main.filename) + '/public/index.html');
});
module.exports = app;