-
Notifications
You must be signed in to change notification settings - Fork 103
/
app.js
executable file
·63 lines (53 loc) · 1.65 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
51
52
53
54
55
56
57
58
59
60
61
62
63
// __
// _________ _____ ____/ /_ __
// / ___/ __ `/ __ \/ __ / / / /
// / /__/ /_/ / / / / /_/ / /_/ /
// \___/\__,_/_/ /_/\__,_/\__, /
// /____/
//
// @brief : a micro bbs system based on duoshuo.com apis
// @author : 新浪微博@郭宇 [turing](http://guoyu.me)
require('babel/register')
// Global dependencies
import express from 'express'
import morgan from "morgan"
import multer from 'multer'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
// Local dependencies
import routes from './routes'
import { findPath } from './libs/utils'
// Local modules
import configs from './configs.json'
const app = express()
const env = process.env.NODE_ENV || 'development'
const production = (env === 'production')
// Environments
app.set('env', env)
app.set('views', configs.views)
app.set('view engine', configs['view engine'])
app.set('port', process.env.PORT || 3000)
app.set('view cache', production)
// Middlewares
app.use(morgan(production ? configs.log : 'dev'))
app.use(bodyParser.urlencoded({ extended: false })) // `application/x-www-form-urlencoded`
app.use(bodyParser.json()) // `application/json`
app.use(multer({ dest: configs.uploads }))
app.use(cookieParser(configs.secret))
app.use(express.static(findPath(process.cwd(), './public')))
// Locals
app.locals.URI = production ?
(configs.URI || 'http://127.0.0.1') :
`http://127.0.0.1:${ app.get('port') }`;
// Connect to database
connect(configs.db)
.then(() => {
// Init routes
routes(app)
// Run
app.listen(app.get('port'), () =>
console.log('Candy is running now'))
})
.catch(err => {
throw err
})