1.使用内置http搭建服务
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {
'Content-Type': 'text/html; charset=utf-8'
});
res.write('hello nodejs');
res.end();
});
server.listen(3000);
console.log('服务启动成功,地址: http://127.0.0.1:3000');
2.基于express模块搭建服务
const express = require('express');
const app = express();
app.get('/', function(req, res) {
res.send('hello nodejs');
});
app.listen(3000, () => {
console.log('服务启动成功! http://localhost:3000');
});