如何在没有路由的情况下在Express上运行多个Apps?

时间:2017-02-14 15:54:32

标签: node.js express node-http-proxy http-proxy-middleware node-https

我希望将2个应用添加到我的个人资料网站,该网站本身就是一个快速应用。

我想在protected function RunQuery ($sql) { $pdo = $this->conn; try { $stmt = $pdo->prepare($sql); if ($stmt) { $stmt->execute($sql); } else { print_r("Unable to prepare the query"); } } catch (PDOException $e) { print_r($e); exit(0); } } 路线下运行这些应用,以便我们可以/projectslocalhost/projects/app1

我希望每个应用的所有子路由都在各自的路由上处理  例如,路线localhost/projects/app2在成功/projects/app1/signup到仪表板后重定向,但现在我需要确保这不会重定向到POST,而是localhost/dashboard

我知道路由,我也在App1上使用它,即

localhost/projects/app1/dashboard

我想要实现这样的事情

var rank = require('./routes/ranks');
...
var taxiRanks = new rank();
...
app.get('/',taxiRanks.findNearbyRanks);
app.get('/whereami',taxiRanks.getCurrentLocation )
app.post('/location',taxiRanks.receiveLocation) 
app.post('/signup',taxiRanks.newUser)

1 个答案:

答案 0 :(得分:2)

您可以使用Express的路由器API:

// app1.js
const app1Routes = express.Router();

app1Routes
  .use('/user', userRoutes)
  .use('/activity', activityRoutes)

module.exports = app1Routes;


// app2.js
const app2Routes = express.Router();

app2Routes
  .use('/food', foodRoutes)
  .use('/candy', candyRoutes)

module.exports = app2Routes;

// app.js
const app1Routes = require('./app1.js');
const app2Routes = require('./app2.js');

app.use('/app1', app1Routes);
app.use('/app2', app2Routes);

例如在userRoutes中,你可以声明你的路线:

const routes = express.Router();

routes.get('/:id', (req, res) => res.status(200).send({ id : 1, firstName : 'junk' }));

routes.post('/', (req, res) => res.status(201).send({ msg : 'User created' }));

module.exports = routes;