Integrate Express.js-REST-Endpoint with Meteor Application

前端 未结 1 1252
囚心锁ツ
囚心锁ツ 2020-12-20 01:07

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 reas

相关标签:
1条回答
  • 2020-12-20 01:40

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

    Et voilà!

    0 讨论(0)
提交回复
热议问题