Angular 2 cli with Express js [英] Angular 2 cli with Express js
问题描述
我想使用express js w / node js作为我的服务器的一个角度2项目。我正在看教程集成快速js w /角度cli(我遵循此和此 )我没有运气。有没有人有任何建议如何做到这一点?目前我有一个新的项目w / cli只是一些说应用程序工程!并希望尝试使用express而不是使用lite服务器。
任何帮助都不胜感激!
这个链接适用,但有一些变化。
我创建一个名为node_server的文件夹/ / li >
这是我的server.js文件:
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('端口'),function(){console.log('app running on port',app.get('port'));});
我希望这对您有用。
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!
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:
- I create a folder called node_server
- 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.
- 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.
- 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.
这篇关于Angular 2 cli with Express js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!