带有 Express js 的 Angular 2 cli [英] Angular 2 cli with Express js

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

问题描述

我想使用带有节点 js 的 express js 作为 angular 2 项目的服务器.我正在查看用于集成带有 angular cli 的 express js 的教程(我遵循了 这个这个),我没有运气.有没有人对如何做到这一点有任何建议?目前我有一个带有 cli 的新项目,上面写着应用程序有效!"并想尝试使用 express 而不是使用 lite 服务器来做到这一点.

感谢任何帮助!

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 对我有用,但有一些变化.

this link works for me, but with some changes.

我安装了 angular-cli 并创建了新项目,然后我按照链接的教程进行操作,但进行了以下更改:

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

  1. 我创建了一个名为 node_server 的文件夹
  2. 在我的项目的根文件夹中运行 npm install express --save .这会在 package.json 中添加类似依赖项的服务器,而无需在 node_server 上创建新的.
  3. 我在 node_server 中创建了一个名为 server.js 的新文件,其中包含一个基本的 Express 服务器.这个服务器只返回dist文件夹的index.html.
  4. 将脚本添加到 package.json 中,脚本与链接教程中的脚本相同,但有一处更改,文件名为 server.js 而不是 index.js

这是我的 server.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'));
});

我希望这对你有用.

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

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