[英]Node.js server issue - added “/” when looking for files in browser
I'm trying to write my first Nodejs server for getting to know Angular/Node and eventually the whole MEAN stack. 我正在尝试编写我的第一台Nodejs服务器,以了解Angular / Node并最终了解整个MEAN堆栈。
My server is running but there's a problem in my code, for some reason when I enter a non existing file, it should redirect to 404, but it doesn't. 我的服务器正在运行,但是我的代码有问题,由于某种原因,当我输入一个不存在的文件时,它应该重定向到404,但事实并非如此。 For some reason the URL gets a double dash;
由于某种原因,URL出现了双破折号;
How would I go about making the redirect to 404 work? 我将如何进行重定向到404的工作?
Here is my code for the server so far. 到目前为止,这是我的服务器代码。
var http = require('http'), fs = require('fs'), path = require('path'), root = __dirname + '/public/', //magic var mime = require('mime'); //Server var server = http.createServer(function (req, res) { // Check is root is queried var fileName = ''; var url = req.url; if (url === '/'){ url = 'index.html'; // redirect when no file specified } fileName = root + url; // check if file exists fs.exists(fileName, function(exists){ if (exists) { serveFile(fileName); // yes } else { path = root + '404.html'; //no serveFile(fileName); } }) //serve file function serveFile(requestFile) { // maak a stream based on events var stream = fs.createReadStream(requestFile); res.writeHead(200, {'Content-Type': mime.lookup(requestFile)}); stream.on('data', function (chunk){ res.write(chunk); }); stream.on('end', function(){ res.end(); }); stream.on('error', function(err){ console.log('error: '+ err); }); } }); server.listen(3000); //server start console.log('Server gestart op http://localhost:3000 ');
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>angular</title> <link href="https://cdn.jsdelivr.net/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> <link href="styles/app.css" rel="stylesheet"/> </head> <body ng-app class="bg"> <h1>First name?</h1> <input type="text" placeholder="Type your name" ng-model='firstName' class="input-lg"/> <p> Hi, {{firstName}} </p> <img src="https://static.pexels.com/photos/7720/night-animal-dog-pet.jpg" height="100px" width="100px"/> </body> <script type="text/javascript" src="https://cdn.jsdelivr.net/angularjs/1.5.5/angular.min.js"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/angularjs/1.5.5/angular.min.js"></script> </html>
Could anyone tell me what's going wrong here? 谁能告诉我这是怎么回事? Thanks in advance!
提前致谢!
公开后删除'/'
:
root = __dirname + '/public'
It is the default behaviour of Node JS. 这是Node JS的默认行为。 ie.
即。 If you request for xxx.com/sample.txt, then the req.url will be "/sample.txt".
如果您请求xxx.com/sample.txt,则req.url将为“ /sample.txt”。
https://nodejs.org/api/http.html#http_message_url https://nodejs.org/api/http.html#http_message_url
So you have consider that in your code, as @Jordan mentioned, remove the "/". 因此,您已经考虑到在代码中(如@Jordan所述),删除“ /”。
Your redirect also should work fine. 您的重定向也应该工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.