1.初始化一个项目
mkdir test
cd test
npm init -y
2.安装node的express框架
npm i --save express
3.起一个简单的服务
- 利用http模块创建一个服务
在项目里创建一个hello.js文件。放入一下代码。
const http = require('http');
const server = http.createServer((req,res) => {
res.end('hello world');
})
server.listen(8080,() =>{
console.log('Server listening on : http://localhost:%s',8080);
})
用node hello.js运行它,访问http://localhost:8080,你会看到 hello world。
- 利用express框架创建一个服务
在项目里创建一个server.js文件。放入一下代码。
const express = require('express');
const app = express();
app.get('/',(req,res)=>{
res.end('hello world');
})
app.listen(3000,()=>{
console.log('Express web app on http://localhost:3000')
})
用npm start运行它,访问http://localhost:3000,你会看到 hello world。