简体   繁体   English

如何使用Node.js服务器部署Angular 5应用

[英]How to deploy Angular 5 app with Node.js Server

I've written an Angular 5 application which gets its data from a node.js server. 我已经编写了一个Angular 5应用程序,该应用程序从node.js服务器获取数据。 I have been able to deploy the application to my web server (which will run node.js at FastComet) but the server doesn't seem to work. 我已经能够将应用程序部署到我的Web服务器(它将在FastComet上运行node.js),但是该服务器似乎无法正常工作。 I'm not sure if I have to deploy that separately, or if that gets included as part of the main ng build. 我不确定是否必须单独部署它,或者是否将其作为主要ng构建的一部分包含在内。

Here are my settings so that you can see, just in case they help: 这是我的设置,以便您查看,以防它们有所帮助:

package.json package.json

{
  "name": "paracordcalc",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "concurrently --kill-others \"ng build --watch --no-delete-output-path\" \"node server.js\"",
    "build": "ng build && node server.js",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --prod --base-href /para-calc/"
  },
  "private": true,
  "dependencies": {
    "@angular/cli": "1.5.0",
    "@angular/compiler-cli": "~5.0.0",
    "@angular/animations": "~5.0.0",
    "@angular/common": "~5.0.0",
    "@angular/compiler": "~5.0.0",
    "@angular/core": "~5.0.0",
    "@angular/forms": "~5.0.0",
    "@angular/http": "~5.0.0",
    "@angular/platform-browser": "~5.0.0",
    "@angular/platform-browser-dynamic": "~5.0.0",
    "@angular/router": "~5.0.0",
    "body-parser": "~1.18.2",
    "bootstrap": "~3.3.7",
    "cookie-parser": "~1.4.3",
    "core-js": "~2.4.1",
    "debug": "~3.1.0",
    "express": "~4.16.2",
    "jade": "~1.11.0",
    "morgan": "~1.9.0",
    "ngx-image-gallery": "^1.3.1",
    "rxjs": "~5.5.2",
    "serve-favicon": "~2.4.5",
    "zone.js": "~0.8.14"
  },
  "devDependencies": {
    "@angular/language-service": "~5.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.2.0",
    "concurrently": "~3.5.0",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "~1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "~0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.2.0",
    "tslint": "~5.7.0",
    "typescript": "~2.4.2"
  }
}

server.js server.js

    var express = require('express');
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');

    var routes = require('./server/routes/index');
    var items = require('./server/routes/items');
    var config = require('./server/routes/config');
    var errors = require('./server/routes/errors');

    var app = express();

    // view engine setup
    app.set('views', path.join(__dirname, '/server/views'));
    app.set('view engine', 'jade');

    app.use(favicon(path.join(__dirname, 'dist/favicon.ico')));
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'dist')));

    // app.use('/', routes);
    app.use('/api/config', config);
    app.use('/api/items', items);
    app.use('/api/errors', errors);
    app.get('*', function(req, res) {
    res.sendFile(path.join(__dirname, 'dist/index.html'));
    });

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
        var err = new Error('Not Found');
        err.status = 404;
        next(err);
    });

    // error handlers

    // development error handler
    // will print stacktrace
    if (app.get('env') === 'development') {
        app.use(function(err, req, res, next) {
            res.status(err.status || 500);
            res.render('error', {
                message: err.message,
                error: err
            });
        });
    }

    // production error handler
    // no stacktraces leaked to user
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: {}
        });
    });

    var debug = require('debug')('server');

    app.set('port', process.env.PORT || 3000);

    app.listen(app.get('port'));

    console.log('Listening on port: ' + app.get('port'));

    module.exports = app;

Here's a screenshot of what my server code structure looks like: 这是我的服务器代码结构的屏幕截图:

在此处输入图片说明

Am I Missing Anything? 我有什么想念吗?

It all runs ok in debug on my machine. 在我的机器上的调试中一切正常。 I'm not sure if the node server is being deployed and just not run on the server, or what I need to do. 我不确定节点服务器是否正在部署并且仅不在服务器上运行,或者我需要做什么。 I've Googled a LOT to try and find an answer and I can't see anything that makes sense. 我已经在Google上搜索了很多,试图找到答案,但看不到任何有意义的内容。

I'm also trying to embed it into a Wordpress website page, which is another problem I can't find help for either, but that's for another discussion another day. 我还试图将其嵌入到Wordpress网站页面中,这是另一个我都找不到帮助的问题,但这是另一天的讨论。 I'm most likely going to try to embed it in an iFrame. 我很可能会尝试将其嵌入到iFrame中。

Any help that you can offer on deploying and running the server would be greatly appreciated. 您将在部署和运行服务器方面提供的任何帮助将不胜感激。 I've only been developing in Angular for about 3 months. 我仅在Angular中开发了大约3个月。

Put your server.js file and node_module folder in parallel to src directory inside angular app. 将您的server.js文件和node_module文件夹平行于angular应用程序中的src目录。 So that it will be able hear nodejs operations of your angular project. 这样它就可以听到您的Angular项目的nodejs操作。

Here in this picture app.js is same as your server.js file. 这张图片中的app.js与您的server.js文件相同。 follow this hierarchy of architecture. 遵循这种体系结构层次结构。 Hope it will help you out. 希望它能对您有所帮助。

在此处输入图片说明

From what I understand the problem here is with starting the NodeJS server. 据我了解,这里的问题是启动NodeJS服务器。 Could you please provide the commands that you are using via console and of course the output, respectively the error that the server returns. 您能否通过控制台提供您正在使用的命令,当然还要提供输出,以及服务器返回的错误。 This I believe will show clearly what the reason for this problem actually is. 我相信这将清楚地表明导致此问题的真正原因。

My best guess is that the ng binary is not deployed on the server which you can get resolve by contacting the FastComet Technical support team. 我最好的猜测是ng二进制文件未部署在服务器上,您可以通过与FastComet技术支持团队联系来解决。 I have heard that they are pretty responsive :) 我听说他们反应灵敏:)

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

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