首页 > 解决方案 > 如何在 AWS 中运行 json-server?

问题描述

我的团队在 AWS 中部署了一个 Angular 应用程序。我们使用 json-server 为应用程序制作模拟服务和数据库。但是,我们找不到在 aws 中运行 json-server 的方法。任何人都可以在这方面帮助我们吗?

这是管道代码-

version: 0.0
os: linux
version: 0.2

env:
    variables:
        S3_BUCKET: "initial-codedeploy-bucket-us-east-1-jready"
        APP_NAME: "shopping-city"
        BUILD_ENV : "prod"

phases:
    install:
        commands:
        # Download and Install NodeJS 10.0
        - curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
        - sudo apt-get install -y nodejs
        - echo Installing source NPM dependencies...
        # Install http drivers for node
        - sudo apt-get update -y
        - sudo apt-get install -y apt-transport-https
        # Install Yarn Package Manager (Replace the commands below if you using NPM).
        # - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
        # - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
        - sudo apt-get update -y
        - sudo apt-get install -y npm
        # Install Angular CLI, If you are not using Angular 6, install angular/cli@1.7.0 or lower, confirm by running ng -v inside your project folder
        - npm global add @angular/cli@6.0.8
        # Install node dependancies.
        - npm install

build:
        commands:
        # Builds Angular application. You can also build using custom environment here like mock or staging
        - echo Build started on `date`
        - ng build --${BUILD_ENV}

post_build:
        commands:
        # Clear S3 bucket.
        - aws s3 rm s3://${S3_BUCKET} --recursive
        - echo S3 bucket is cleared.
        # Copy dist folder to S3 bucket, As of Angular 6, builds are stored inside an app folder in distribution and not at the root of the dist folder
        - aws s3 cp dist s3://${S3_BUCKET}/${APP_NAME} --recursive
        - echo Build completed on `date`

artifacts:
    files:
        - '/'
    discard-paths: yes
    base-directory: 'dist*'

标签: node.jsangularamazon-web-servicesjson-server

解决方案


如果要在 AWS EÇ2 上运行 json-server,则需要运行以下命令:

nohup json-server --watch data/store.json --port 

然后您可以使用 curl 来验证 json-server 是否已启动并且可以正确响应请求。

如果要使用低于 1024 的端口,请使用sudo.

我无法在公网调用api,可能是安全组问题或其他一些权限设置不正确。但我希望这能有所帮助。


推荐阅读