-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
59 lines (50 loc) · 1.94 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
/**
* @file App 入口 / Commonjs module
* @module server
* @author GuoGuang <https://github.com/GuoGuang>
*/
const http = require('http')
const express = require('express')
const { Nuxt, Builder } = require('nuxt')
const { isProdMode, isDevMode, environment } = require('./environment')
process.noDeprecation = true
// 替换 console 为更统一友好的
/* const { log, warn, info } = console
const color = c => isDevMode ? c : ''
global.console = Object.assign(console, {
log: (...args) => log('[log]', ...args),
warn: (...args) => warn(color('\x1b[33m%s\x1b[0m'), '[warn]', '[madaoo.com]', ...args),
info: (...args) => info(color('\x1b[34m%s\x1b[0m'), '[info]', '[madaoo.com]', ...args),
error: (...args) => info(color('\x1b[31m%s\x1b[0m'), '[error]', '[madaoo.com]', ...args),
}) */
const config = require('./nuxt.config')
const port = environment.PORT || 3000
// const host = isProdMode ? (environment.HOST || '0.0.0.0') : '0.0.0.0' // 生产必须是0.0.0.0?
const host = isProdMode ? (environment.HOST || '127.0.0.1') : '0.0.0.0'
const app = express()
const nuxt = new Nuxt(config)
const server = new http.Server(app)
if (config.dev) {
const handleProxy = path => (req, res) => {
const targetUrl = 'http://' + req.url.replace('/proxy/' + (path ? path + '/' : ''), '')
require('request').get(targetUrl).pipe(res)
}
console.log(handleProxy)
console.log('handleProxy')
app.get('/proxy/bilibili/*', handleProxy('bilibili'))
app.get('/proxy/*', handleProxy)
}
app.use(nuxt.render)
app.set('port', port)
const bootstrap = () => {
server.listen(port, host)
const appName = '码道'
const envText = isDevMode ? '开发模式' : '生产模式'
console.info(`${appName} ${envText}启动成功!listening on ${host}:${port}, at ${new Date().toLocaleString()}`)
}
config.dev
? new Builder(nuxt).build().then(bootstrap).catch((error) => {
console.error('开发模式启动失败:', error)
process.exit(1)
})
: bootstrap()