- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 express js 构建一个应用程序,它将有不同的客户端,如网络和移动。我不想同时使用一个应用程序,因为一些中间件会带来额外的负担。比如 session 中间件。那么一个项目有没有可能有两个应用程序。它是如何工作的?
最佳答案
你在express中制作的app
对象是一个function(req,res,next),适合Express自己的中间件链。因此,您可以使用 app.use
将与前导路径片段匹配的请求发送到在别处定义的应用。
文档:http://expressjs.com/api.html#app.use
$ npm install express
//mobile.js
var app = require('express')();
app.get('/', function(req, res){
res.send('Mobile Route')
});
module.exports = app;
//desktopApp.js
var http = require('http');
var express = require('express');
var desktopApp = express();
var mobileApp = require('./mobile.js');
desktopApp.use('/mobile', mobileApp)
desktopApp.use(desktopApp.router);
desktopApp.use(express.errorHandler());
desktopApp.get('/', function(req, res){
res.send('Desktop Route')
});
desktopApp.get('/mobile', function(req, res){
// Because Express respects the order that you set up the middleware chain,
// the mobileApp `/mobile` route gets first dibs to send a response or next()
res.send('Inaccessible Desktop Route')
});
desktopApp.get('/mobile/foobar', function(req, res){
// When mobileApp can't find any suitable route matching this path, it gives
// up, and desktopApp continues to pass the request down the middleware stack.
// It ends up matching this route, where we send a response
res.send('Desktop Route')
});
http.createServer(desktopApp).listen(3000, function(){
console.log('Listening on 3000');
});
// Results
$ curl localhost:3000/
Desktop Route
$ curl localhost:3000/mobile/
Mobile Route
关于node.js - expressjs 中的两个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17233068/
基于此答案的问题:https://stackoverflow.com/a/18650183/4478897 我试图找到这个解决方案,但似乎没有任何效果符合我的需要。 集群 expressjs 和 so
我有一个使用 Sequelize.js 作为 ORM 运行的快速应用程序。我的 express 应用程序从我的主要 Rails 应用程序接收请求,并且由于跨域策略,这些请求是使用 getJSON 执行
如何处理同步错误并保证在同一代码中出现错误? 这是我的代码,但不确定其工作正常,有什么想法吗? helpers.list({ limit: 1 })
在我的 ExpressJS 代码中,我限制主体大小如下: app.use(bodyParser.urlencoded({ limit : "512kb", extended: fals
我有一个看起来像这样的对象: editinvite: { _id: '', title: '', codes_list: [] } 问题是我无法在 NodeJS 应用程序中访问codes_
我正在尝试让我的nodeapp 使用express 工作。 我已经通过 nginx 设置了反向代理以使用 https://dank.ml/api/v1但每当我启动我的应用程序时,它都不想检测 / 响应
我完全失去了理智: 我有一个快速应用程序:以下是此示例的几个文件的快照: 应用程序.js -模型 --Event.js --Match.js 路线 --matches.js app.js: globa
我有我的nodejs项目,我想将记录器与我的route.js分开以获得干净的代码。 但是我遇到了下一个问题,当我尝试将记录器加载到route.js 文件时,它会显示下一个错误: TypeError:
我在 MEAN 应用程序中的模板缓存方面遇到一些困难。我有一个导航栏,它使用条件逻辑来显示/隐藏向用户显示的按钮。当用户点击页面时,这些值将被设置为 null 或 false,但是一旦他们登录,这些值
是否可以根据可选路线创建不同的查询? app.get('/:genre/:book?', function (req, res) { var genre = req.params.genre; v
我正在尝试学习 ExpressJS,并且遇到了这段代码。我似乎无法理解 app.use 函数,并且文档对我来说也不清楚。当调用 app.use 时,此特定示例代码中的/public 目录到底发生了什么
我正在开发一个学校管理系统。当老师发送发布请求时,我在后端创建一个路由内的全局对象。所以当多个老师访问服务器时,会创建多个全局对象......或者每个老师访问同一个对象?(会被重写同一个对象吗?) 最
我有一个用 typescript 编写的小型微服务,在 AKS 上的 kubernetes 集群中运行。我使用 Helm 生成了入口 apiVersion: extensions/v1beta1 ki
我需要将 JSON 发送到我的服务器,但 POST 请求显示为空正文。这是我在前端的 typescript : function sendData() : void { console.log("Se
这是我的 app.js import path from 'path'; import bodyParser from 'body-parser'; import express from 'expr
我一直在使用 ExpressJS 来促进文件上传,但据我所知,您可以将文件上传到任何路由,无论它是否处理它。 说我有 app.post('/photos/upload', photos.upload)
我正在阅读 Express.JS 4.x API,并且很好奇他们是如何设置的。这是我对正在发生的事情的理解:在 Express.JS 4.x API 的示例代码中,express 模块被导入并分配给变
我有以下注册页面 form(method='post', action='users/register') .form-group label Name inp
我有诸如 :id/comments/、:id/otherRoute 等路线。对于这些路线,我需要创建新的 Controller 。例如: let id: number = +req.params["i
嘿,我有一个使用 Express.js 和 Handlebars 运行的网站,该网站的导航栏和侧边栏在所有页面上都保持相同。当我路由时,它仅更改页面容器 {{{body}}}。 有没有一种方法可以让我
我是一名优秀的程序员,十分优秀!