Is it possible to set a base URL for NodeJS app?

前端 未结 7 545
有刺的猬
有刺的猬 2020-11-29 19:51

I want to be able to host multiple NodeJS apps under the same domain, without using sub-domains (like google.com/reader instead of images.google.com). The problem is that I\

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 20:17

    Just to update the thread, now with Express.js v4 you can do it without using express-namespace:

    var express = require('express'),
        forumRouter = express.Router(),
        threadRouter = express.Router(),
        app = express();
    
    forumRouter.get('/:id)', function(req, res){
      res.send('GET forum ' + req.params.id);
    });
    
    forumRouter.get('/:id/edit', function(req, res){
      res.send('GET forum ' + req.params.id + ' edit page');
    });
    
    
    forumRouter.delete('/:id', function(req, res){
      res.send('DELETE forum ' + req.params.id);
    });
    
    app.use('/forum', forumRouter);
    
    threadRouter.get('/:id/thread/:tid', function(req, res){
      res.send('GET forum ' + req.params.id + ' thread ' + req.params.tid);
    });
    
    forumRouter.use('/', threadRouter);
    
    app.listen(app.get("port") || 3000);
    

    Cheers!

提交回复
热议问题