-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (72 loc) · 2.5 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
78
79
80
81
82
83
84
85
86
87
88
89
90
const next = require('next')
const express = require('express')
const { parse } = require('url')
const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')
const bodyParser = require('body-parser')
const fileUpload = require('express-fileupload')
const getConfig = require('next/config').default
const _ = require('lodash')
const cors = require('cors')
const rest = require('./src/server/rest')
// const specialUrls = ['/_next/static', '/sw.js']
const startExpress = (options, callback) => {
// console.info('startExpress', curFolder)
const app = next(options)
const handle = app.getRequestHandler()
const BASE_PATH = process.env.BASE_PATH || '';
app
.prepare()
.then(() => {
if (callback) {
callback()
return
}
const server = express()
server.use(
bodyParser.urlencoded({
extended: false
})
)
server.use((req, res, next) => {
// console.log('req.url1: ', req.url, BASE_PATH)
if (BASE_PATH !== '') {
if (req.url === '/') {
req.url = BASE_PATH
}
}
// console.log('req.url2: ', req.url)
next();
});
// 支持跨域,nsgm export 之后前后分离
server.use(cors())
server.use(bodyParser.json())
server.use(fileUpload())
server.use('/static', express.static(path.join(__dirname, 'public')))
server.use('/rest', rest)
const nextConfig = getConfig()
const { publicRuntimeConfig } = nextConfig
const { host, port, prefix } = publicRuntimeConfig
if (prefix !== '') {
server.use(prefix + '/static', express.static(path.join(__dirname, 'public')))
server.use(prefix + '/rest', rest)
}
server.get('*', (req, res) => {
const { url } = req
// console.log('url: ' + url)
const parsedUrl = parse(url, true)
return handle(req, res, parsedUrl)
})
server.listen(port, () => {
console.log('> Ready on http://' + host + ':' + port)
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})
}
module.exports = {
startExpress
}