这次给大家带来如何做出node.js界面,做出node.js界面的注意事项有哪些,下面就是实战案例,一起来看一下。
最近要写一个工具界面整合项目的功能属性,方便其他部门的人进行编辑,有点类似后台。会有部分数据上的交互。于是学习了下node.js后端的知识。
源码如下:
// filename:myServer.js
// a simple http server
var
fs = require('fs'),
url = require('url'),
path = require('path'),
http = require('http');
//从命令行参数获取root目录,默认是当前目录
var root = path.resolve(process.argv[2] || '.');
console.log('Static root dir: ' + root);
//创建服务器
var server = http.createServer(function (request, response) {
//获取URL的路径
var pathname = url.parse(request.url).pathname,
//获取对应的本地文件的路径
filepath = path.join(root, pathname);
//获取文件状态
fs.stat(filepath, function (err, stats) {
if (!err && stats.isFile()) {
console.log('200 ' + request.url);
//发送响应
response.writeHead(200);
//将文件流导向response
fs.createReadStream(filepath).pipe(response);
} else {
console.log('404 ' + request.url);
response.writeHead(404);
response.end('404 Not Found');
}
});
});
server.listen(8081);
console.log('Server is running at http://127.0.0.1:8081/');本项目目录结构如下:

js和html页面按常规写好之后执行cnd命令:

最终效果如下:

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
小计天空 企业管理 asp版1、本程序由小计天空出品 版权所有2、本程序为免费程序,不得依任何商业形式转送而得到暴利包含产品,新闻,留言,招聘等众多功能,源码全,功能全,100%可使用。界面清新自然后台地址 admin/admin admin
0
以上就是如何做出node.js界面的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号