我有一个关于我的代码nodejs服务器的问题,当它发送一个头但在控制台中我正确地aparesca时,浏览器将其忽略,并将所有内容都视为text / html,在这里我向它们添加路由器的代码是什么
var path = require("path");
var fs = require('fs');
var root = "www";
function enrutar(manejador,ruta,respuesta){
console.log("Voy a rutear algo para "+ruta);
var extencion = path.extname(ruta);
console.log("la extencion es: ",extencion);
if(typeof manejador[ruta] === 'function'){
manejador[ruta](respuesta);
}
else
{
switch (extencion)
{
// get the html
case '.html':
fs.readFile(root + ruta, function (err, data) {
if (err) return send404(respuesta);
respuesta.writeHead(200, { 'content-Type': 'text/html' });
respuesta.write(data);
respuesta.end();
});
break;
// get the script that /public/chatclient.html references
case '.js':
fs.readFile(root + ruta, function (err, data) {
if (err) return send404(respuesta);
respuesta.writeHead(200, { 'content-Type': 'text/javascript' });
respuesta.write(data);
respuesta.end();
});
break;
// get the styles that /public/chatclient.html references
case '.css':
fs.readFile(root + ruta, function (err, data) {
if (err) return send404(respuesta);
respuesta.writeHead(200, { "Content-type" : "text/css"});
respuesta.end(data);
console.log("Sirvo css: ",respuesta);
});
break;
case '.json':
fs.readFile(root + ruta, function (err, data) {
if (err) return send404(respuesta);
respuesta.writeHead(200, { 'content-Type': 'application/json' });
respuesta.write(data);
respuesta.end();
});
break;
case '.jpg':
fs.readFile(root + ruta, function (err, data) {
if (err) return send404(respuesta);
respuesta.writeHead(200, {'content-Type': 'image/jpeg'});
respuesta.write(data);
respuesta.end();
});
break;
case '.mp3':
fs.readFile(root + ruta, function (err, data) {
if (err) return send404(respuesta);
respuesta.writeHead(200, {'content-Type': 'text/html'});
respuesta.write(data);
respuesta.end();
});
break;
case '.png':
fs.readFile(root + ruta, function (err, data) {
if (err) return send404(respuesta);
respuesta.writeHead(200, {'content-Type': 'image/png'});
respuesta.write(data);
respuesta.end();
});
break;
default: send404(res);
}
}
var registro = fs.createWriteStream('registro.txt',{'flags':'a'});
registro.write(ruta + '\n');
}
function send404(res){
res.writeHead(404, {'Content-Type': 'text/html'});
res.write("404 Objeto no encontrado :c", 'utf8');
res.end();
}
exports.enrutar = enrutar;
这是我的服务器代码:
var servidor = require('http');
var url = require('url');
function iniciar(enrutar, manejador){
function arrancaServidor(peticion,respuesta){
var ruta = url.parse(peticion.url).pathname.replace("%20"," ");
if(ruta == "/"){
ruta = "index.html";
}
var contenido = enrutar(manejador,ruta,respuesta);
}
servidor.createServer(arrancaServidor).listen(80);
}
exports.iniciar = iniciar;
所有答案都有这样的标题:
Remote Address:127.0.0.1:80
Request URL:http://localhost/css/style.css
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/css,*/*;q=0.1
Accept-Encoding:gzip,deflate,sdch
Accept-Language:es-419,es;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Cookie:_manga_session=WkNYb1AvMFJUME9qVmRrSUtHbXBZbnRwQXRrUndZL01SeUE2K1BwYkh5dGt6M25xSllwcXk2THhWSjZpWUFQV1cyZzcxamRnTzVqMFBFRUlrNHAwM1NQWU9ML1dvY2V6TkZESnpETmhueUdQZFMyWDJZdDhLVmt2cVlvWVBmMTRHYmhIejZieW9rS0lGQWowbTJ5OXdnPT0tLTJyWksxa2w5N0dwQVVaVFp0bkRkYXc9PQ%3D%3D--069e1756464d40f36e80a86f386e9cab9f9cd014; PHPSESSID=1aj908onjk1g3muvkanhoq4s10
Host:localhost
Pragma:no-cache
Referer:http://localhost/demo.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Response Headersview source
Connection:keep-alive
Content-Type:text/html
Date:Fri, 04 Jul 2014 08:17:17 GMT
Transfer-Encoding:chunked
在这个问题上花了几天时间,我开始感到绝望哈哈哈,非常感谢。
PD:对不起,我的英语不好。
我的回答是,将服务器运行到Packete,Supervisor
以避免每次更改都必须手动启动它。不确定是否Supervisor
具有某种缓存,因为在运行服务器时node --debug
(几乎重新安装了已安装的浏览器后,拼死了,请理解我哈哈哈),它可以工作。
不知道这是否使用不正确,Supervisor
因为据我说,请检查更改server.js整个文件夹是否丢失或仅更改文件...任一种方式都非常感谢那些承担阅读我的问题并保持这一工作的人工作质量 :)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句