2
votes

I've installed Angular 2 Webpack which includes a sample app and demonstrates routing. What I want to look into is using Angular2 for the front end routing but to the use ExpressJS for a RESTful API backend, but on the same server i.e.

http://localhost:3000/#/ will serve the front end in Angular http://localhost:3000/api will serve the back end API in Express

If I create a api/index.html I can see this shows the api home page but I haven't got a clue how to create the 'app.js' for express to get the express routing working.

I appreciate I could easily do it by running two instances but I was trying to keep it all self contained.

Regards

1
The docs are pretty straightforward for routing. You might want to check out express generator for easier scaffolding of your server. - Vivek Pradhan

1 Answers

0
votes

I couldn't find a way to do it on the same port so I went with placing this in config/webpack.common.js. (I had thought I would need to start a separate 'npm' process for the API but clearly not!)

var express = require('express');
var app2 = express();

app2.get('/', function (req, res) {
  res.send('API');
});

app2.listen(3001, function () {
});