nodejs 创建 html,用nodeJS写第一个页面

背景:今年剩下的几个月要学习后端开发.使用nodejs加mysql写后端接口.先接触起来,用nodejs写一个页面.

第一步,在E盘创建个nodeJS文件夹,里面有两个文件夹,app和server

第二步,在server文件夹下运行命令

npm init //初始化 npm,一直回车即可

npm install express --save

第三步:打开vscode编译器,把nodeJS文件打开,然后在server文件夹下创建index.js文件.

(编译器有很多,我习惯用vscode了.用其他的打开也是一样的.)

index.js代码内容如下:

const express = require(express);

const path = require(path);

const app = express();

// 在 app 文件夹开启静态服务

app.use(express.static(../app));

app.listen(8080, () => {

console.log(Demo server listening on port 8080);

});

第四步:在app文件夹下创建一个index.html文件

index.heml内容如下:

hello world! 我是nodeJS的html页面

第五步:在server文件夹下运行命令:

node index.js

第六步:用浏览器打开端口显示页面:

背景:今年剩下的几个月要学习后端开发.使用nodejs加mysql写后端接口.先接触起来,用nodejs写一个页面. 第一步,在E盘创建个nodeJS文件夹,里面有两个文件夹,app和server 第二步,在server文件夹下运行命令 npm init //初始化 npm,一直回车即可 npm install express --save 第三步:打开vscode编译器,把nodeJS文件打开,然后在server文件夹下创建index.js文件. (编译器有很多,我习惯用vscode了.用其他的打开也是一样的.) index.js代码内容如下: const express = require(express); const path = require(path); const app = express(); // 在 app 文件夹开启静态服务 app.use(express.static(../app)); app.listen(8080, () => { console.log(Demo server listening on port 8080); }); 第四步:在app文件夹下创建一个index.html文件 index.heml内容如下: hello world! 我是nodeJS的html页面 第五步:在server文件夹下运行命令: node index.js 第六步:用浏览器打开端口显示页面:
经验分享 程序员 微信小程序 职场和发展