【问题标题】:AWS Deploy my NodeJS express appAWS 部署我的 NodeJS express 应用程序
【发布时间】:2018-09-14 12:34:45
【问题描述】:

以下是我的./server.js,当我在终端中node server.js 时,我的角度分布在./client/dist,我的角度应用程序和nodejs 后端按预期工作。现在如何在 aws beanstalk 上部署(我愿意更改 beanstalk)?

大多数教程都希望我从头开始工作,但我确实需要服务器像在 localhost 上那样工作,如下所示。

const express = require('express');
const colors = require('colors');
const bodyParser = require('body-parser');
const compression = require('compression');
const path = require('path');
const fs = require('fs');
const cors = require('cors');


// init "app"
const app = express();
var staticRoot = __dirname + '/client/dist/';
app.set('port', (process.env.PORT || 5000));

app.use(cors({origin: `http://localhost:4200`}));

//parse incoming data before routes
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

// api routes
app.use('/api',require('./api/api'));


app.use(function(req, res, next) {
    //if the request is not html then move along
    var accept = req.accepts('html', 'json', 'xml');
    if (accept !== 'html') {
        return next();
    }
    // if the request has a '.' assume that it's for a file, move along
    var ext = path.extname(req.path);
    if (ext !== '') {
        return next();
    }
    fs.createReadStream(staticRoot + 'index.html').pipe(res);
});



app.use(express.static(staticRoot));

app.listen(app.get('port'), function() {
    console.log('app running on port', app.get('port'));
});

【问题讨论】:

  • 我不会减一;但是,请改为评论。这个问题和角度有什么关系...
  • 拥有 Angular 应用程序的人可能知道如何使用节点后端部署它们?

标签: node.js express amazon-ec2 amazon-elastic-beanstalk


【解决方案1】:

我创建了一个包含完整教程和源代码的现代 MEAN Stack 指南。特别是针对您的问题,我创建了一个分步指南,介绍如何将 MEAN 堆栈应用程序部署到 AWS Elastic Beanstalk (https://www.meankit.io/guides/deploy-with-aws)

如果您需要更多信息,还有参考链接。

【讨论】:

  • 我认为这会起作用.. 我不确定 YARN。我现在不使用它。 ng build 将制作角度分布,但我不确定如何制作 server-dist
  • 您不需要有dist-server 文件夹。我在该项目中使用了 Angular Universal,这就是该文件夹存在的原因。您需要做的就是运行ng build --prod - 无需使用纱线。重要的是将已经编译好的 dist 文件夹上传到 AWS Elastic Beanstalk
猜你喜欢
  • 2017-05-06
  • 1970-01-01
  • 2018-12-02
  • 2016-12-03
  • 2019-07-22
  • 2021-06-24
  • 2021-03-18
  • 2020-07-08
  • 2021-07-19
相关资源
最近更新 更多