我的应用程序已准备好分发,我决定使用 NodeWebkit。现在,问题是我的应用程序加载了本地文件,为此我需要一个 Web 服务器。如何将 NodeWebkit 和一些 Web 服务器打包在一起?如果无法将 Web 服务器添加到 NodeWebkit,如何将文件(图像、脚本、css)加载到 html 中?谢谢!:)
setTimeout(function() {
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 8888;
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);
fs.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
}).listen(parseInt(port, 10));
}, 1000);