node.js静态资源怎么获取

2025-02-03 16:52:06
推荐回答(1个)
回答1:

let http = require("http"),
url = require("url");

let staticPath = "./res/";

let app = http.createServer((request, response) => {
let pathName = url.parse(request.url).pathname;

response.write("pathName = " + pathName);
response.end();
});

app.listen(80, "127.0.0.1");

此时,在客户端请求地址譬如 http:127.0.0.1/res/1.html ,客户端则会显示: pathName = /res/1.html 。