[英]Static HTTP File Server in NodeJs: Sometimes external .js and .css files load properly, sometimes they don't?
编辑:我知道使用express或其他方法会更容易,但是这都是学习性的练习,如果抱歉,这很麻烦,哈哈!
编辑2:似乎(在添加了一些控制台日志以进行调试之后)看来,该问题与以下事实有关:浏览器向服务器发出一个请求(例如,style.css)时,它发出了另一个请求(例如,对于login-fail.js),在完成第一个请求的响应之前。 看来来自浏览器的这些多个请求会引起某种问题,每个后续请求都会阻止前一个请求的完成。 kes,我需要帮助。
编辑3:经过调试,似乎pathname变量不会在每次请求时更改其值。 出于某种原因,路径名的值在每个请求中均保持不变,这使每个请求的响应都相同-仍然很陌生,uri的值在每个请求中都发生了变化(而uri是赋予路径名其值的东西...)仍在尝试找出为什么发生这种奇怪的行为。
因此,当服务器请求链接到特定.html文件的外部.js和.css文件时,我一直遇到这个问题。 响应始终不一致。 例如,有时代码将完美运行,而其他时候将加载css,而不是js,有时两者都加载,有时两者都不加载。 我无法确定这是因为我的代码是同步的还是其他原因。 这是我的代码:
Server.js
//Module requires
var http = require("http"),
fs = require("fs"),
path = require('path'),
url = require('url'),
invoke = require("./invoke");
//Object "MIMETYPES"
//Maps relationships between file extensions and their respective MIME Content-Types
var MIMETYPES = {
".html": "text/html",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".js": "text/javascript",
".css": "text/css"
};
//Object "invokeOptions"
//Options passed to Invoke() function for POST requests
var invokeOptions = {
postData : "",
uri : ""
}
var PORT = 8888;
//HTTP Server Begin
http.createServer(function(req, res) {
var uri = url.parse(req.url).pathname;
pathname = path.resolve(__dirname, "..") + uri;
console.log("Recieved " + req.method + " request for : " + uri);
invokeOptions.uri = uri;
//GET requests wrapper
if (req.method == "GET"){
//Invoke() function handler for GET requests
if (path.extname(pathname) == ""){
invoke.invoke(invokeOptions, req, res);
return;
}
//Static file server for GET requests
fs.exists(pathname, function(exists) {
if(!exists) {
console.log("Requested file \'" + pathname + "\' doesn't exist.");
res.writeHead(404, {'Content-Type': 'text/plain'});
res.write('404 Not Found\n');
res.end();
return;
}
var contentType = MIMETYPES[path.extname(pathname)];
res.writeHead(200, {"Content-Type" : contentType});
console.log("Current URI: " + uri + " has content type: " + contentType);
fs.createReadStream(pathname).pipe(res);
return;
});
}
//POST requests wrapper
if (req.method == "POST"){
var postData = "";
req.on("data", function(postPacket) {
postData += postPacket;
});
req.on("end", function() {
invokeOptions.postData = postData;
invoke.invoke(invokeOptions, req, res);
return;
});
}
}).listen(PORT);
console.log ("Server listening on port: " + PORT);
Invoke.js-处理非文件请求,即服务器上的功能请求
var fs = require("fs"),
querystring = require("querystring"),
path = require("path");
function invoke (options, req, res){
process.stdout.write("Invoking function --> ");
if (options.uri == "/"){
console.log("Index");
res.writeHead(200, {"Content-Type" : "text/html"});
fs.createReadStream("../index.html").pipe(res);
return;
}
if (options.uri == "/login"){
console.log("Login");
fs.readFile(path.resolve("../users.json"), "UTF-8", function(err, data){
if (err) throw err;
var json = JSON.parse(data);
var user = querystring.parse(options.postData).username,
password = querystring.parse(options.postData).password;
console.log("Submitted Username: " + user + "\nSubmitted Password: " + password);
if (json.users[0].username == user && json.users[0].password == password){
res.writeHead(200, {"Content-Type" : "text/html"});
fs.createReadStream("../app.html").pipe(res);
return;
}
else {
res.writeHead(300, {"Content-Type" : "text/html"});
fs.createReadStream("../login-fail.html").pipe(res);
return;
}
});
}
else {
console.log("Error! Bad request.");
res.writeHead(400, {"Content-Type" : "text/plain"});
res.end("Error 400: Bad Request. \nThere is no function corresponding to that request.");
}
}
exports.invoke = invoke;
Login-fail.js-这是几乎不会加载的代码
$(document).ready(function() {
var current = 3;
var countdown = $(".countdown");
function down (){
current--;
if (current != 0){
countdown.text(current);
}
else {
clearInterval(interval);
window.location.replace("./");
}
}
var interval = setInterval(down, 1000);
});
基本上,index.html文件是一种接受用户名和密码的形式,将提交的POST数据与json文件进行比较,如果与json文件中的哈希值匹配,则它请求app.html,否则请求登录失败。 html。 调用login-html文件时,它已链接到css和js,而这些请求几乎从未运行过!
另外,我认为应该注意的是,当请求CSS时,console.logs的“ content-type”为“ text / javascript”,但不起作用。 任何帮助将不胜感激!
哇靠。
并不是每个请求都将路径名声明为变量,因为我使用了;
而不是,
女士们和男士们,我现在要死了。
您在login-fail.html
中使用的相对路径可能无法正确解析,因为URL路径未更改( /login
),因此浏览器正在查找/login/css/style.css
和/login/js/login-fail.js
。 尝试修改您的login-fail.html
以使用绝对路径而不是相对路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.