1.新建serve.js
var PORT = 8080; //端口
var DIR = 'test1'; //用于存放html的目录var http = require('http');
var url=require('url');
var fs=require('fs');
var mine=require('./mine').types;
var path=require('path');var server = http.createServer(function (request, response) {var pathname = url.parse(request.url).pathname;var realPath = path.join(DIR, pathname);//console.log(realPath);var ext = path.extname(realPath);ext = ext ? ext.slice(1) : 'unknown';fs.exists(realPath, function (exists) {if (!exists) {response.writeHead(404, {'Content-Type': 'text/plain'});response.write("This request URL " + pathname + " was not found on this server.");response.end();} else {fs.readFile(realPath, "binary", function (err, file) {if (err) {response.writeHead(500, {'Content-Type': 'text/plain'});response.end(err);} else {var contentType = mine[ext] || "text/plain";response.writeHead(200, {'Content-Type': contentType});response.write(file, "binary");response.end();}});}});
});
server.listen(PORT);
console.log("Server runing at port: " + PORT + ".");
2新建mine.js
exports.types = {"css": "text/css","gif": "image/gif","html": "text/html","ico": "image/x-icon","jpeg": "image/jpeg","jpg": "image/jpeg","js": "text/javascript","json": "application/json","pdf": "application/pdf","png": "image/png","svg": "image/svg+xml","swf": "application/x-shockwave-flash","tiff": "image/tiff","txt": "text/plain","wav": "audio/x-wav","wma": "audio/x-ms-wma","wmv": "video/x-ms-wmv","xml": "text/xml"
};3. 在当前目录运行 node serve.js```
详细解决方案
node 搭建web服务
热度:98 发布时间:2023-09-14 17:44:37.0
相关解决方案
- Node.css("background-image""url('images/e.gif')")中的url是干什么用的啊该如何处理
- Node.Tag 点不出来,该怎么解决
- widonws下,ERLANG node 与 C node 的连接建立有关问题
- 360的两款杀毒软件区别(node 32(半年免费) 与360杀毒(永久免费))解决思路
- C# XML创办<node />格式的节点
- C# XML创造<node />格式的节点
- 装配和卸载WebLogic Node Manager服务
- 自学Node.js: WebStorm+Node.js开发环境的配备
- No appropriate node to show Web View for "/WebModule/"but the web ser
- node.js child_process simple operctor 二
- NodeJS 多进程运行后,socket.io 应该怎么使用?与 Node 单进程时一样吗
- Node.js学习(15)-Express 3.x跟Express 2.x区别
- Node.js学习(六)-事件
- Node.js操作Mysql事务有关问题
- Node.js express 版本出现的几个常见有关问题
- Ubuntu 停安装 git 和 node.js
- Node.js 测试批处理下令
- node.js 累加文件
- node.js施行提交操作时候报错
- Node JS 汉语文档
- node.js mysql-native 判断结果聚合大小
- node.js为何不用coroutine
- 获取跟修改text node
- Node cannot be inserted at the specified point in the hierarchy" code: "三
- Node.js简介:可独力于浏览器,运行于服务器的Javascript
- 读《Node.js开发指南》,对Node.js的将来趋势 之愚见
- Node.js从入门到深入――Node.js的理解
- node 创设
- Node.js Cannot find module 'xxx' 有关问题解决
- node.js upload file 下传文件