admin管理员组文章数量:1366402
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).
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.
What I want is to access the "normal" Site via for example and at the same time be able to access my REST-Endpoint via for example
/
.
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.
Is this somehow possible? :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);
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).
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.
What I want is to access the "normal" Site via for example http://myfancysite./my-display-route
and at the same time be able to access my REST-Endpoint via for example http://myfancysite./api/insert-crazy-data/
.
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.
Is this somehow possible? :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);
Share
asked Aug 29, 2017 at 11:46
RockettomatooRockettomatoo
2777 silver badges18 bronze badges
1 Answer
Reset to default 16Meteor 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);
Et voilà!
本文标签: javascriptIntegrate ExpressjsRESTEndpoint with Meteor ApplicationStack Overflow
版权声明:本文标题:javascript - Integrate Express.js-REST-Endpoint with Meteor Application - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743687982a2522217.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论