javascript - 将路线从 app.js 移至 Route.js 并且应用程序不再工作

标签 javascript node.js express

在我的 app.js 文件中,我有下面的代码,它按预期工作。我需要清理我的代码,所以我将其移至它自己的路由(routes/random),但它不再起作用,因为我收到一条错误消息:“http://localhost:1337/random/1/1/testing 404(未找到)”,我不确定为什么。我的原始代码在我的 app.js 文件中工作时是:

app.get('/random/:room/:userId/:message', function(req, res) {
    fs.appendFile(room.number.toString(), req.params.message, function(err) {
        if (err) {
            console.log('error writing messages to file');
        };
        fs.readFile('./' + room.number, 'utf-8', function(err, data) {
            if (err) {
                if (err.fileNotFound) {
                    return this.sendErrorMessage('can\'t find the file, you linked it incorrectly');
                }
                console.log('error reading message file');
            };

            if (req.params.userId == 1) {
                messages.user1.push(data);
            } else {
                messages.user2.push(data);
            };
            console.log(messages);
            res.send(data);
            fs.unlink(req.params.room, function(err) {
            });

        });
    });
});
<小时/>

新代码包括 app.js 的以下内容

<小时/>
var express = require('express');
var app = express();
var fs = require('fs');
var random = require('./routes/random');

app.use('/random/', random);
app.use(express.static('public'));
app.use(express.static('public/js'));
app.use(express.static('public/images'));
<小时/>

我移动它后,路由代码是:

<小时/>
var express = require ('express');
var fs = require ('fs');
var random = express.Router();

random.get('/random/:room/:userId/:message', function(req, res) {
    fs.appendFile(room.number.toString(), req.params.message, function(err) {
        if (err) {
            console.log('error writing messages to file');
        };
        fs.readFile('./' + room.number, 'utf-8', function(err, data) {
            if (err) {
                if (err.fileNotFound) {
                    return this.sendErrorMessage('can\'t find the file, you linked it incorrectly');
                }
                console.log('error reading message file');
            };

            if (req.params.userId == 1) {
                messages.user1.push(data);
            } else {
                messages.user2.push(data);
            };
            console.log(messages);
            res.send(data);
            fs.unlink(req.params.room, function(err) {
            });

        });
    });
});


module.exports = random;

谁能解释一下我做错了什么,导致它无法找到该文件?

最佳答案

在代码中,您在 random.js 中定义了一个名为 random\random... 的路由,删除其中的第一个 random ,因为中间件(app.use..)会将所有带有/random 的路由定向到您的路由器实例。

关于javascript - 将路线从 app.js 移至 Route.js 并且应用程序不再工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32834600/

相关文章:

javascript - ImageData 数组中的索引逻辑是什么?

javascript - 将 bootstrap 添加到 angular2-seed 元素的 webpack 包中

node.js - MongoDB/Mongoose 的 E11000 重复键错误

javascript - 将 Angular 路由与预定义的服务器端路由同步

node.js - 使用 azure 应用服务的 Nodejs Express Router REST API

node.js - 使用 Express 从 S3 流式传输文件,包括有关长度和文件类型的信息

javascript - 使用 Javascript 从下拉列表中选择 jQuery 框架选择选项

javascript - 如何从内部拒绝 promise 然后发挥作用

javascript - 在 Node.js 中提供静态文件 - 500 内部服务器错误

node.js - nodejs + pg 用回调做 CRUD