config.js
const config = {
hostname: '127.0.0.1', //IP
port: 3000 //端口号
}
exports.config = config
serve.js
const http = require('http') //引用系统提供的http
const config = require('./config').config // 引用config文件
const server = http.createServer((req, res) => { //http模块的一个方法,实例化一个服务
res.statusCode = 200 // 执行成功状态码
res.setHeader('Content-Type', 'text/plain')//http传输协议
res.end('HELLO WORLD \n') //在用户浏览器打印
})
//运行服务器
server.listen(config.port, config.hostname, () => {
console.log(`Server running at http://${config.hostname}:${config.port}`)// 在控制台打印
})