node 从0开始学习
1,新建一个文件夹,
2, 进入文件夹内部,执行 npm init 一路回车
3,创建index.js
4,安装express及重要插件
先来用express这个框架, 终端分别输入
npm install express –save (注:–save是保存到项目中的意思)
npm install body-parser –save (注:express必要的中间件)
5、编写index.js
//将一下内容拷进index.js中
const path = require('path') //引入当前路径
const express = require('express') //引入express
const bodyParser = require('body-parser')//引入body-parser
const app = express();
app.use(bodyParser.urlencoded({extended:true}));
let _static = path.join('webapp'); //定义静态文件的目录,此处只为确认node环境运行了
app.use(express.static(_static));
app.listen(4000); //监听端口
console.log('服务器启动,端口:4000');
6、创建webapp文件夹,并在文件夹内创建index.html
//index.html内容
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
恭喜你成功了!
</body>
</html>
node环境就算搭建完成了
终端运行 node index.js看看什么样子吧!
版权声明:本文由Web学习之路发布,如需转载请注明出处。