UNPKG

1.81 kBJavaScriptView Raw
1let http = require('http')
2const fs = require('fs')
3const path = require('path')
4let querystring = require('querystring')
5const bodyHandle = require('./lib/page')
6
7const corePath = path.join(__dirname, 'core')
8http.createServer(function (req, res) {
9 //暂存请求体信息
10 let body = ""
11
12 //请求链接
13 // console.log(req.url);
14
15 //每当接收到请求体数据,累加到post中
16 req.on('data', function (chunk) {
17 body += chunk; //一定要使用+=,如果body=chunk,因为请求favicon.ico,body会等于{}
18 // console.log("chunk:",chunk)
19 })
20
21 //在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。
22 req.on('end', function () {
23 console.log(body)
24 // 处理body
25 const dom = bodyHandle(body, {
26 "root": "/src",
27 "entry": "servertemple0",
28 "headFolder": "head",
29 "outFolder": "dist",
30 "autoPack": true,
31 "minifyCss": false,
32 "minifyJs": false,
33 "pageFolder": "page"
34 })
35
36 // 根据不同情况使用不同的core
37 // 读取出核心代码
38 let coreScript = fs.readFileSync(path.join(corePath, 'main.js'), 'utf8')
39 if (dom.isOnePage) {
40 // 单页面
41 coreScript += fs.readFileSync(path.join(corePath, 'SinglePage.js'), 'utf8')
42 } else {
43 // 多页面
44 coreScript += fs.readFileSync(path.join(corePath, 'MultiPage.js'), 'utf8')
45 }
46 // 页面切换特效
47 coreScript += fs.readFileSync(path.join(corePath, 'animation.js'), 'utf8')
48 // 整合页面代码
49 dom.script = coreScript + dom.script
50 // 允许跨域
51 res.setHeader('Access-Control-Allow-Origin', '*')
52 res.write(JSON.stringify(dom))
53 // console.log(dom)
54 res.end()
55 })
56}).listen(3000)
\No newline at end of file