使用极简NodeJs代码实现一个http静态服务器 , 且能够访问并引用当前目录下的HTML/CSS/JS等静态资源

代码 :javascript

const http = require('http');
const express = require('express');
const app = express();
const port = 8088;
app.use("/", express.static(__dirname + '/'));

// 建立服务端
http.createServer(app).listen(port, function () {
    console.log(`服务器启动成功,能够经过 http://localhost:${port}/ 来访问`)
});

使用方法 :html

一、把该数据内容复制到一个JS文件中,如“app.js”;
二、把“app.js”文件放到须要做为服务器的目录下,文件发到“public”目录下;
三、运行命令“node app.js”,若没有安装express模块,运行命令“npm install express”进行安装;
四、打开浏览器,访问服务器:http://localhost:8088/index.html 便可java

使用极简NodeJs代码实现一个http静态服务器node


访问结果 :express

image.png

image.png

相关文章
相关标签/搜索