将 Express.js-REST-Endpoint 与 Meteor 应用程序集成 [英] Integrate Express.js-REST-Endpoint with Meteor Application

查看:14
本文介绍了将 Express.js-REST-Endpoint 与 Meteor 应用程序集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个棘手的情况:我目前正在构建一个完整的具有流星功能的应用程序.但出于自动化原因,我还需要将一些功能作为 REST-Service 公开(第三方应用程序应该能够通过 REST 插入和接收数据).

I have a kinda tricky situation: I'm currently building a full meteor-featured application. But I also need to expose some functionality as REST-Service for automation reasons (a third party application should be able to insert and receive data via REST).

express.js-package 似乎是将 REST-Endpoint 构建到 node.js 环境中的一个非常可靠的选择,但我想知道如何将此端点集成到meteor 中.

The express.js-package seems to be a very solid option for building a REST-Endpoint into a node.js environement but I'm wondering how to integrate this endpoint into meteor.

我想要的是通过例如 http://myfancysite.com/my-display-route 访问普通"站点,同时能够访问我的 REST-Endpoint例如通过 http://myfancysite.com/api/insert-crazy-data/.

What I want is to access the "normal" Site via for example http://myfancysite.com/my-display-route and at the same time be able to access my REST-Endpoint via for example http://myfancysite.com/api/insert-crazy-data/.

普通"站点可通过启动 Meteor 时定义的端口访问.问题是,我必须为 express.js 指定一个不同的端口 来侦听,并且我希望(meteor 和 express)共享同一个端口,因为我不想访问 REST- 不同端口上的端点.

The "normal" Site is accessible via the port defined when starting Meteor. The thing is, that I have to specify a different port for express.js to listen on and I want both - meteor and express - to share the same port since I don't want to access the REST-Endpoint on a different port.

这有可能吗?:D

这是我目前用于 express 的一些代码.

Here's some code I use for express at the moment.

//<meteor-root>servermain.jsx

import { Meteor } from 'meteor/meteor';

// do some meteor things
...

//require express
var express = require('express');

//create application
var app = express();

//use environement defined port or 3000
var port = process.env.PORT || 3000;

//create router
var router = express.Router();

//define routes
...

//register all routes with '/api'
app.use('/api', router);

//start server
app.listen(port); // <= this should be the same port as the meteor application itself!
console.log('listening on port ' + port);

推荐答案

Meteor 本质上是一个节点应用程序,它已经公开了一个 连接 http服务器,这意味着您可以简单地定义服务器路由:

Meteor is essentially a node app that already exposes a connect http server, which means you can define server routes simply like:

import { WebApp } from 'meteor/webapp';

WebApp.connectHandlers.use('/hello', (req, res, next) => {
  res.writeHead(200);
  res.end('Hello world from your server');
});

如果你坚持使用express,那么你可以像这样将你的express路由注册为connect中间件:

If you insist on using express, then you can register your express routes as connect middleware like so:

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import express from 'express';

const app = express();

app.get('/api', (req, res) => {
  res.status(200).json({ message: 'Hello from Express!!!'});
});

WebApp.connectHandlers.use(app);

等等!

这篇关于将 Express.js-REST-Endpoint 与 Meteor 应用程序集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆