S
S
Skrolea2016-03-03 10:53:43
Angular
Skrolea, 2016-03-03 10:53:43

How to connect Angular and Node.js( Express)?

Good afternoon I am
using angular2-seed to develop an application with the following structure:
d646171f56a54ad7834ece68279c4eae.png
I made folders from the root "client" and "server". In the server, after all the manipulations with the tsd install express -sfile server.ts
, I put the following in it

import express = require('express');
import path = require('path');
var port: number = process.env.PORT || 3000;
var app = express();

var renderIndex = (req: express.Request, res: express.Response) => {
    res.sendFile(path.resolve(__dirname, '../client/dist/dev/index.html'));
}

app.get('/*', renderIndex);

var server = app.listen(port, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log('This express app is listening on port:' + port);
});

In the case of running server.ts, I get errors
733200656a3a4b36bbf2e6b5ea08d2e1.png
. As I understand it, this is due to the fact that it is not written where to get static files from. Something like
app.use('/folder', express.static(path.resolve(__dirname, '../client/dist/prod/folder/')))

It’s not clear to me, in the case of dev and prod, which folders to specify in
app.use('/folder', express.static(path.resolve(__dirname, '../client/dist/dev/folder/')))

to make it work?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ayan Bai, 2016-03-03
@wolf47

I started using mean.io. Cool solution, mean means mongo express angular nodejs.
The main things are already implemented in the kernel, you just have to make your own package

B
bromzh, 2016-03-03
@bromzh

here is a working starter-kit: https://github.com/angular/universal-starter
and tsd is dead, take typings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question