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

Ale*_*lls 1 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);
});
Run Code Online (Sandbox Code Playgroud)

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

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;
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是我的功能似乎没有将响应传递给浏览器.浏览器没有显示数据来自index.html的证据,index.html是一个准系统.html HTML5文件.

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

jfr*_*d00 6

对于初学者:

if (requestUrl.pathname = '/')
Run Code Online (Sandbox Code Playgroud)

应该:

if (requestUrl.pathname === '/')
Run Code Online (Sandbox Code Playgroud)

你的代码是分配,而不是比较.


另外,它.pipe()是异步的,但是你正在调用res.end()BEFORE它在你的finally{}块中完成它的工作,它会关闭响应流并阻止你的管道做任何事情.默认情况下,它.pipe()会自动关闭写入流,因此res.end()在使用时根本不需要.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;
Run Code Online (Sandbox Code Playgroud)