我试图找出一个基本的Node.js服务器。我希望它在客户端尝试连接到“根” URL(正确的术语是btw?)时发送index.html
和styles.css
文件。这是代码。
var http = require ('http');
var fs = require ('fs');
function send404response(response){
response.writeHead(404, {"Content-Type":"text/plain"});
response.write("page not found");
response.end();
}
function onRequest (request, response) {
if(request.method == "GET" && request.url == '/'){
response.writeHead(200, {"Content-Type":"text/html"});
fs.createReadStream("./index.html").pipe(response);
response.writeHead(200, {"Content-Type":"text/css"});
fs.createReadStream("./styles.css").pipe(response);
}else{
send404response(response);
}
}
http.createServer(onRequest).listen(8888)
console.log('the server is running...');
styles.css
无法加载。客户端错误:GET http://localhost:8888/styles.css net::ERR_ABORTED 404 (Not Found)
答案 0 :(得分:1)
我想我知道问题出在哪里。因此,您正在将每个请求发送到函数onRequest
,即使链接标记请求也到达了那里。由于IF条件的值为false,因此它会发送您指定的404错误。尝试使用快递服务器并创建路由,或添加其他条件以检查该文件是否存在于路径中(如果存在),然后返回该文件作为响应。
答案 1 :(得分:0)
在我的特定情况下有效的实现,我使用了swich
构造来处理不同的请求:
var http = require ('http');
var fs = require ('fs');
function send404response(response){
response.writeHead(404, {"Content-Type":"text/plain"});
response.write("page not found");
response.end();
}
function onRequest (request, response) {
if(request.method == "GET"){
console.log("request URL is: " + request.url);
switch (request.url){
case "/":
response.writeHead(200, {"Content-Type":"text/html"});
fs.createReadStream("./index.html").pipe(response);
break;
case "/styles.css":
response.writeHead(200, {"Content-Type":"text/css"});
fs.createReadStream("./styles.css").pipe(response);
break;
}
}else{
send404response(response);
}
}
http.createServer(onRequest).listen(8888)
console.log('the server is running...');
答案 2 :(得分:0)
int base = 1;
for (int line = 0; line < n; line++)
{
int sum = base + line;
for (int col = 0; col < n - line; col++)
{
sum = sum + col;
printf("%d ", sum);
sum = sum + line;
}
base = base + line + 1;
printf("\n");
}
在上面的代码片段中,function onRequest (request, response) {
if(request.method == "GET" && request.url == '/'){
response.writeHead(200, {"Content-Type":"text/html"});
fs.createReadStream("./index.html").pipe(response);
response.writeHead(200, {"Content-Type":"text/css"});
fs.createReadStream("./styles.css").pipe(response);
}else{
send404response(response);
}
}
条件存在问题。您只检查if
网址。当您的html文件尝试加载CSS时,它将向服务器发出GET请求,并且请求url为/
。但是在上面的代码中,您根本不允许路由。根据您的逻辑,您仅提供/cssFileName.css
网址,所有其他路由都给/
错误。因此,您还需要考虑404
网址来提供您的CSS文件。因此理想情况下,您的代码应该像这样...
/cssFileName.css
但理想情况下,对于所有CSS和素材资源(图片,视频),都应从静态文件夹中提供。因此,请改用function onRequest (request, response) {
if(request.method == "GET" && request.url == '/'){
response.writeHead(200, {"Content-Type":"text/html"});
fs.createReadStream("./index.html").pipe(response);
} else if (request.method == "GET" && request.url == '/cssFileName.css')
response.writeHead(200, {"Content-Type":"text/css"});
fs.createReadStream("./styles.css").pipe(response);
}else{
send404response(response);
}
}
。供参考https://expressjs.com/en/starter/static-files.html