富贵资源网 Design By www.hznty.com
在windows上用nodejs搭建一个静态文件服务器,即使你一点基础没有也能学会nodejs静态文件服务器的搭建,本文介绍的非常详细,很适合零基础入门的朋友学习。
首先安装nodejs:
"htmlcode">
node -v npm -v
如果得到了版本号则表示nodejs安装完成
"htmlcode">
npm config set registry https://registry.npm.taobao.org
以后安装nodejs模块 都会从淘宝的npm镜像中下载
"htmlcode">
npm config set registry https://registry.npmjs.org
接下来搭建静态文件服务器
"htmlcode">
var server = require('./server.js'); var rootpath = 'root'; var sv = server.create({ port: '9587', host: '127.0.0.1', root: rootpath }); "css": "text/css", "less": "text/css", "gif": "image/gif", "html": "text/html", "ejs": "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", "default": "text/plain" }; module.exports = function (ext) { return types[ext] || 'text/plain' }
"htmlcode">
var http = require('http'); var path = require('path'); var fs = require('fs'); var url = require("url"); var mime = require('./mime.js'); function getPromise(cbk) { return (new Promise(cbk)); } exports.create = function (opts) { var root = opts.root; var sv = http.createServer(); function request(request, response) { var pathname = decodeURIComponent(url.parse(request.url).pathname); var realPath = path.resolve(path.join(root, pathname));//请求的实际路径 getPromise(function (resolve, reject) { fs.exists(realPath, function (isExists) {//判断路径是否存在 isExists "file"><a href="${item}">${item}</a></li>`); } else if (stat.isDirectory()) { resolve(`<li class="dir"><a href="${item}/">${item}</a></li>`); } else { resolve(''); } }) })); }); Promise.all(pmlist).then(function (linkList) { var links = '<ul>'; links += '<li class="dir"><a href="../">../</a></li>'; links += linkList.join(''); links += '</ul>'; var dirPage = ` <!doctype html> <html> <head> <meta charset="utf-8"/> <style> a{color:blue;text-decoration: none;} .dir a{color:orange} </style> </head> <body> ${links} </body> </html> `; resWrite(response, '200', 'html', dirPage); }); }).catch(function (err) { resWrite(response, '500', 'default', err.toString()); }) } }) } else {//既不是文件也不是文件夹 resWrite(response, '404', 'html', '<h1>404</h1>file or dir : <h3>' + pathname + '</h3>not found'); } }).catch(function (err) { resWrite(response, '500', 'default', err.toString()); }) }) } sv.on('request', request); sv.listen(opts.port, opts.host); return sv; }; function resFile(response, realPath) {//输出一个文件 fs.readFile(realPath, function (err, data) { if (err) { resWrite(response, '500', 'default', err.toString()); } else { var ext = path.extname(realPath).toLocaleLowerCase(); ext = (ext "htmlcode">node index.js以上所述是小编给大家介绍的在windows上用nodejs搭建静态文件服务器的简单方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...