结构:

代码:
const express = require('express');
const fs = require('fs');
const cp = require('child_process');
const process = require('process');
const app = express();
app.get('/', (req, res) => {
fs.readFile('./lianxi.json', 'utf-8', (err, data) => {
if (err) throw err
res.send(data)
})
})
app.listen(8080, () => {
// console.log(process.platform); 这个 可以打印 系统的信息
console.log('serve runing at http://127.0.0.1:8080');
if (process.platform == 'win32') {
cp.exec('start http://127.0.0.1:8080') //这个 是为了 能够在保存的时候 打开网页
} else {
console.log('can not support derwin');
}
})
该博客展示了如何使用Node.js的Express库创建一个简单的HTTP服务器,监听8080端口,并读取并返回'lianxi.json'文件内容。在Windows平台上,服务器启动时会尝试打开对应的URL,而在非Windows系统上则提示不支持。

被折叠的 条评论
为什么被折叠?



