一、初始化npm
在项目文件夹目录里运行cmd命令
F:\网课学习\前端工程师\第29-30周>npm init
运行结束后会生成package.json文件;
二、新建一个test.js文件并写入代码
const http=require("http");
const server=http.createServer((req,res)=>{
res.end("hello ,this is server")//返回给前端的内容
})
server.listen(3000)//监听端口3000
console.log("监听到了HTTP请求");//服务端自己的控制台输出
三、CMD命令启动服务
在项目文件夹目录下面启动cmd,运行命令
F:\网课学习\前端工程师\第29-30周>node test.js
四、测试
在网页上输入地址:http://localhost:3000/ ,回车后可以看到网页显示如下内容,一个简易的服务端就完成了。
hello ,this is server