Angular 2 cli with Express js

Armagetin

I want to use express js w/ node js to be my server for an angular 2 project. I was looking at tutorials for integrating express js w/ the angular cli (I followed this and this) and I had no luck. Does anyone have any suggestions on how to do this? Currently I have a new project up w/ cli just something that says "app works!" and want to try to do it using express as opposed to using the lite server.

any help is appreciated!

silvelo

this link works for me, but with some changes.

I install angular-cli and create new project, then I follow the tutorial of the link but with this changes:

  1. I create a folder called node_server
  2. In run npm install express --save in root folder of my project. This add server like dependencie in your package.json without create new on node_server.
  3. I create a new file in node_server called server.js, with a basic express server. This server only returns the index.html of dist folder.
  4. Added the scripts to the package.json, the scripts are the same in the link tutorial, but with one change, the filename is server.js not index.js

This is my server.js file :

const express = require('express');

var app = express();  
var staticRoot = __dirname;  

app.set('port', (process.env.PORT || 3000));  

app.use(express.static(staticRoot));

app.get('/', function(req, res) {
    res.sendFile('index.html');
});

app.listen(app.get('port'), function() {  
    console.log('app running on port', app.get('port'));
});

I hope this works for you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive