使用Node读取Html

 时间:2024-10-14 21:17:16

1、新建一个文件夹,命名为Express,里面建立一个js文件,我这里命名为load_html.js,建立一个html文件,我命名为test.html.

2、在load_html.js中输入以下内容:var url = require("url"), fs=require(&鳎溻趄酃quot;fs"), http=require("http");http.createServer(function (req, res) { console.log("req.url:"+req.url); var pathname=__dirname+url.parse(req.url).pathname; fs.readFile(pathname,function (err,data){ res.end(data); });}).listen(3000);console.log("Server running at localhost");在test.html中输入以下内容:<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>主页的测试</title></head><body><h1>主页的测试</h1></body></html>

3、打开cmd ,打开Express文件夹,输入 nodeload_html.js,若cmd 输出:Server running at localhost,则表明服务器启动成功。若报错则表明服务器启动失败,请查找原因。

使用Node读取Html

4、服务器启动成功后在浏览器地址栏输入:http://localhost:3000/test.html,则可以看到我们html页面的内容。以上就是我写的简单的例子,希望对你有所帮助。

使用Node读取Html
  • mockplus团队协作功能
  • 微软全新浏览器Edge不播放音乐怎么办?
  • html+css+jQuery文字上下滑动banner特效
  • PHP如何抓取网站或者某个网页的快照图片
  • PHP学习之道:[3]XAMPP配置PHP开发环境
  • 热门搜索
    怎么玩魔方 信怎么写 勋怎么读 坐便堵了怎么疏通 怎么追女生 茄子怎么做 靳东怎么读 腾讯视频怎么登录别人的会员 sarah怎么读人名 王者荣耀怎么退款