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

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

问题描述

我有一个棘手的情况:我正在构建一个完整的流星特色应用程序。但出于自动化原因,我还需要将一些功能作为REST服务公开(第三方应用程序应该能够通过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).

快递。 js-package似乎是一个非常可靠的选项,用于将REST-Endpoint构建到node.js环境中,但我想知道如何将此端点集成到流星中。

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 -Endpoint在另一个端口上。

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

这是我目前用于快递的一些代码。

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

//<meteor-root>\server\main.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本质上是一个已暴露出< a href =https://github.com/senchalabs/connect =nofollow noreferrer> connect 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');
});

如果您坚持使用快递,那么您可以将快速路线注册为连接中间件,如下所示:

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);

Etvoilà!

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

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