简体   繁体   English

如何正确设置Node,Express和Angular2

[英]How to setup Node, Express and Angular2 properly

I am trying to set-up a workspace with Node, Express, Angular2 (Database,- MongoDB or SQL) 我正在尝试使用Node,Express,Angular2(数据库,MongoDB或SQL)设置工作区

But I am not sure how to correctly set it up combined. 但我不确定如何正确组合设置。 My file structure looks like this so far, and I intend to put the front-end under the public folder. 到目前为止,我的文件结构看起来像这样,我打算将前端放在公共文件夹下。

── package.json
├── public
├── routes
│   └── test.js
├── server.js
└── views

So far my my server.js looks like this 到目前为止,我的server.js看起来像这样

var express = require('express');
var mysql = require('mysql');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var path = require('path');

var lel = require('./routes/test');

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/api/', test);

app.use(function(req, res, next) {
    // error handling
});


var server = app.listen(3000, function() {
    // shows the connection etc
});

module.exports = app;

My test.js is where I handle my API calls under /api/test. 我的test.js是我在/ api / test下处理API调用的地方。

And this is how my package.json looks like, with a script for npm, that will start the server with npm start And all the required dependencies, that can be downloaded with npm install 这就是我的package.json的样子,使用npm脚本,它将使用npm start启动服务器,以及所有必需的依赖项,可以通过npm install下载

{
  "name": "testing",
  "version": "0.1.0",
  "description": "-",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "-",
  "license": "-",
  "dependencies": {
    "body-parser": "^1.15.0",
    "cookie-parser": "^1.4.1",
    "ejs": "^2.4.1",
    "express": "^4.13.4",
    "morgan": "^1.7.0",
    "mysql": "^2.10.2",
    "path": "^0.12.7"
  }
}

Now my question is how do I add Angular2 to work properly under public and so they start up together at npm start . 现在我的问题是我如何添加Angular2在公共场合下正常工作,以便它们在npm start一起npm start I have been following the quickstart at Angulars documentation site, and noticed they use lite-server, but that shouldn't be necessary with node/express right? 我一直在关注Angulars文档站点上的快速入门,并注意到它们使用lite-server,但是使用node / express并不需要这样做吗? How should my package.json look like after Angular2 is added. 添加Angular2后,我的package.json应该是什么样子。 For example like this: 例如这样:

├── package.json
├── public
│   ├── app
│   │   ├── app.component.js
│   │   └── main.js
│   └── index.html
├── routes
│   └── test.js
├── server.js
└── views

If it is still too early with Angular2, then I have the same question but with Angular1.X, or perhaps is it better to use Angular2 with TS? 如果使用Angular2还为时过早,那么我对Angular1.X也有相同的疑问,或者将Angular2与TS一起使用会更好吗?

I was curious about the same setup and was able to write a simple, using NG2 RC1, example on how to run NG2 on Express. 我对相同的设置感到好奇,并且能够使用NG2 RC1编写一个简单的示例 ,该示例介绍了如何在Express上运行NG2。 Setup is not straightforward compared to NG1 and the key to NG2 on Express is making sure SystemJS knows where to find the libraries 与NG1相比,设置并不简单,而Express上NG2的关键在于确保SystemJS知道在哪里可以找到这些库

Here are the key things that you should look into. 这是您应该研究的关键事项。 Please note that it is not recommended to expose node_modules. 请注意,不建议公开node_modules。 I want my example to be simple so focus can be easily placed on learning basic setup. 我希望我的示例很简单,以便可以轻松地将重点放在学习基本设置上。

  1. Expose the location of static resources (index.html) and dependencies (node_modules) 公开静态资源(index.html)和依赖项(node_modules)的位置
app.use(express.static(rootPath + '/client/app'))
app.use('/node_modules', express.static(rootPath + '/node_modules'));
  1. Let SystemJS know where to find dependencies 让SystemJS知道在哪里可以找到依赖项
System.config({
    map: {
        "@angular": "node_modules/@angular",
        "rxjs": "node_modules/rxjs"
    },
    packages: {
        '/': {
            //format: 'register',
            defaultExtension: 'js'
        },
        'node_modules/@angular/http': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'http.js'
        },
        'node_modules/@angular/core': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'index.js'
        },
        'node_modules/@angular/router': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'index.js'
        },
        'node_modules/@angular/router-deprecated': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'index.js'
        },
        'node_modules/@angular/platform-browser-dynamic': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'index.js'
        },
        'node_modules/@angular/platform-browser': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'index.js'
        },
        'node_modules/@angular/compiler': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'compiler.js'
        },
        'node_modules/@angular/common': {
            //format: 'cjs',
            defaultExtension: 'js',
            main: 'index.js'
        },
        'rxjs' : {
            defaultExtension: 'js'
        }
    }
});
System.import('./main').then(null, console.error.bind(console));

Please have a look at my example on GitHub 请看看我在GitHub上的示例

您可以使用一个生成器,该生成器将创建基本目录和文件以开始使用。以后,可以通过从npm安装其插件来使用任何数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM