相关疑难解决方法(0)

如何在Express中的多个文件中包含路由处理程序?

在我的NodeJS express应用程序中,我有app.js一些常见的路由.然后在一个wf.js文件中我想定义更多的路线.

如何app.js识别wf.js文件中定义的其他路由处理程序?

一个简单的要求似乎不起作用.

node.js express

199
推荐指数
10
解决办法
17万
查看次数

如何使用NodeJS和Express从app.js中分离路由和模型

我正在使用Node和Express创建一个应用程序.但是,我可以看到很快就很难管理放在里面的所有路线app.js.我已将所有模型放在子目录中/models.

这是我的app当前结构:

app.js
models
  -- products
  -- customers
  -- ...
public
views
node_modules
Run Code Online (Sandbox Code Playgroud)

app.js:

var express = require('express'),
    routes = require('./routes'),
    user = require('./routes/user'),
    http = require('http'),
    path = require('path'),
    EmployeeProvider = require('./models/employeeprovider').EmployeeProvider,
    Products = require('./models/products').Products,
    Orders = require('./models/orders').Orders,
    Customers = require('./models/customers').Customers,
    checkAuth = function(req, res, next) {
      if (!req.session.user_id) {
        res.send('You are not authorized to view this page');
      } else {
        next();
      }
    };

var app = express();
Run Code Online (Sandbox Code Playgroud)

然后一些配置一样port,views目录,渲染引擎等. …

node.js express

32
推荐指数
3
解决办法
6万
查看次数

Express 4.0内容协商和应用结构

我使用 Node Express 4.0 作为我的 Web 服务器和 REST API。我正在使用内容协商来服务器多种类型的数据并处理其余 API 的版本控制。然而,我的界面开始变得非常混乱。下面我有一个非常简单的例子来说明我如何使用内容协商(但是我在每个接受标头中有更多的代码行。

我的问题是,是否有使用大型 Express Rest 应用程序的经验,以及如何构建代码以分离 HTTP 部分和实际数据处理之间的关注点,以避免非常混乱的app.get()app.post()等功能,以及如何保持对整个过程的良好概述应用?

'use strict';

var express = require('express');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.json());

var data = [
    { id: 0, title: 'This is content 0'},
    { id: 1, title: 'This is content 1'},
    { id: 2, title: 'This is content 2'},
    { id: 3, title: 'This is content 3'}
];

app.get('/data', function(req, res) {

    res.format({
        'application/vnd.mydata.ids.v1.0+json': function() …
Run Code Online (Sandbox Code Playgroud)

rest node.js express

5
推荐指数
0
解决办法
1770
查看次数

将快速路由分解为单独的文件

我已经尝试了我在 s/o 上找到的所有答案,我确定我一定遗漏了一些东西。什么对我没有错误而是给了我一个 404。我尝试了来自Node.js 中组织路由的答案,strongloop 的路由分离模式,来自如何在 Express 中的多个文件中包含路由处理程序的答案, 遇到类似Router.use 需要中间件功能的错误但这些答案都没有奏效。在 Express 4.0无法将路由拆分为单独的文件的答案不会出错,但也会出现 404。似乎每个答案都有不同的语法和风格,也许是我混合和匹配不正确?

现在我的 /routes/persons.js 有这个模式:

    var express = require('express');
    var persons = express.Router();

    persons.route('/persons/:user_id')
        .put(function (req, res, next) {
            // etc
    });

    module.exports = persons;       
Run Code Online (Sandbox Code Playgroud)

在我的 server.js 文件中,我有:

    var persons = require('./routes/persons');
    app.use('/persons', persons);
Run Code Online (Sandbox Code Playgroud)

这种组合不会引发错误,但也不会执行任何操作。我尝试将端点添加到 server.js 行:

    var persons = require('./routes/persons');
    app.get('/persons/:user_id', persons.addpersons);
Run Code Online (Sandbox Code Playgroud)

并将 people.js 剥离为仅导出功能:

    exports.addpersons = function (req, res, next) { …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express

4
推荐指数
1
解决办法
2556
查看次数

标签 统计

express ×4

node.js ×4

javascript ×1

rest ×1