javascript - 没有 Express 的简单 Node.js 服务器 - 管道 HTML 文件以响应

标签 javascript node.js

我正在尝试制作一个没有 Express 的简单 Node.js 服务器,在这里我实际上了解了更多关于实际服务器文件和基于路径请求和基本 HTTP 内容的请求数据的肮脏和肮脏。

我有这样一个简单的服务器:

 var http = require('http');

const PORT = 6969;

var allRoutes = require('./routes/all');

var server = http.createServer(allRoutes);

server.listen(PORT, function () {
    console.log("Server listening on: http://localhost:%s", PORT);
});

然后我有一个像这样的“中间件”函数来处理所有请求:

var url = require('url');
var fs = require('fs');
var appRootPath = require('app-root-path');
var path = require('path');

function handleRequest(req, res) {

    var requestUrl = url.parse(req.url);


    var fsPath;
    if (requestUrl.pathname === '/') {
        fsPath = path.resolve(appRootPath + '/view/index.html');
    }
    else {
        fsPath = path.resolve(appRootPath + '/view/' + requestUrl.pathname);
    }

    fs.stat(fsPath, function (err, stat) {

        if (err) {
            console.log('error occurred...' + err);
            return end(req, res);
        }

        try {
            if (stat.isFile()) {
                res.writeHead(200);
                fs.createReadStream(fsPath).pipe(res);
            }
            else {
                res.writeHead(500);
            }
        }
        finally {
            end(req, res);
        }
    });
}


function end(req, res) {
    res.end();
}


module.exports = handleRequest;

我遇到的问题是我的函数似乎没有将响应通过管道传递给浏览器。浏览器没有显示数据来自 index.html 的证据,这是一个简单的 .html HTML5 文件。

我偷了这个例子,很惊讶它并没有真正起作用。有人有想法吗?我确定 fs.stat 函数没有遇到错误,然后它正在流式传输 index.html 文件,它似乎没有流式传输到正确的位置...

最佳答案

对于初学者:

if (requestUrl.pathname = '/')

应该是:

if (requestUrl.pathname === '/')

你的代码是赋值,而不是比较。


此外,.pipe() 是异步的,但是您调用 res.end() 之前它会在您的 finally 中完成它的工作{} block ,它关闭响应流并阻止您的管道执行任何操作。默认情况下 .pipe() 将自行关闭写入流,因此在使用 .pipe( )

您可以将代码更改为:

var url = require('url');
var fs = require('fs');
var appRootPath = require('app-root-path');
var path = require('path');

function handleRequest(req, res) {

    var requestUrl = url.parse(req.url);


    var fsPath;
    if (requestUrl.pathname === '/') {
        fsPath = path.resolve(appRootPath + '/view/index.html');
    }
    else {
        fsPath = path.resolve(appRootPath + '/view/' + requestUrl.pathname);
    }

    fs.stat(fsPath, function (err, stat) {

        if (err) {
            console.log('error occurred...' + err);
            return end(req, res);
        }

        try {
            if (stat.isFile()) {
                res.writeHead(200);
                fs.createReadStream(fsPath).pipe(res);
            }
            else {
                res.writeHead(500);
                end(req.res);
            }
        }
        catch(e) {
            end(req, res);
        }
    });
}


function end(req, res) {
    res.end();
}


module.exports = handleRequest;

关于javascript - 没有 Express 的简单 Node.js 服务器 - 管道 HTML 文件以响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34231741/

相关文章:

node.js - 发送数据流作为响应 - Node.js - 使用 Node.js 的 Azure 文件服务

javascript - Node Express Res 发送文件

javascript - JS 对象值以 'undefined' 开头

javascript - 为什么无法访问父类的同名属性?

Javascript 无法从 header 工作

javascript - 使用 JavaScript 更改 HTML 中的链接文本

javascript - Express,在静态文件中展开变量

node.js - 如何在不使用docker安装Node js的情况下开发Node JS(create-react-app)

javascript - 是否有 ramda 函数可以帮助您将日志记录添加到管道/组合中?

javascript - Bootstrap Ajax 选项卡未预加载