Angular 2 cli with Express js [英] Angular 2 cli with Express js

查看:225
本文介绍了Angular 2 cli with Express js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用express js w / node js作为我的服务器的一个角度2项目。我正在看教程集成快速js w /角度cli(我遵循 )我没有运气。有没有人有任何建议如何做到这一点?目前我有一个新的项目w / cli只是一些说应用程序工程!并希望尝试使用express而不是使用lite服务器。

任何帮助都不胜感激!

解决方案

这个链接适用,但有一些变化。



我创建一个名为node_server的文件夹/ / li >

  • 在运行npm中安装express - 在我的项目的根文件夹中保存。这个添加服务器就像你的package.json中的依赖关系,而在node_server上没有创建新的。

  • 我在node_server中创建一个名为server.js的新文件,带有一个基本的express服务器。该服务器只返回dist文件夹的index.html。

  • 将脚本添加到package.json中,脚本在链接教程中是相同的,但是一个更改,文件名是server.js not index.js

  • 这是我的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:

    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.

    这篇关于Angular 2 cli with Express js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆