How to run json-server in AWS? - node.js

My team has deployed an Angular application in AWS. We used json-server for making mock service and database for the application. But, we can't find a way to run json-server in aws. Can anyone help us in this regard?
This is the pipeline code-
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*'

If you want to run json-server on the AWS EÇ2, you need to run the following command:
nohup json-server --watch data/store.json --port
Then you can use curl to verify that the json-server is up and can respond to the request correctly.
If you want to use a port lower than 1024, run the command using sudo.
I can't call the api in the public network, maybe the issue of security group or some other permission isn't set properly. But I wish this can help.

Related

Deploying React app on Debian 11 with serve throws unexplainable error in the CI/CD Pipeline on gitlab

The following as the pipeline we are using to deploy the App to the Debian server.
stages:
- deploy
deploy-job: # This job runs in the deploy stage.
stage: deploy # It only runs when *both* jobs in the test stage complete successfully.
environment: production
image: node:latest
before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
script:
- echo "Deploying application..."
- ssh $SSH_USER#$SSH_IP "cd $PROEJCT_PATH/$PROJECT_DIRECTORY_NAME && serve -s build"
- echo "Application successfully deployed."
But this throws the following Error Message:
file:///usr/local/lib/node_modules/serve/build/main.js:169
const ipAddress = request.socket.remoteAddress?.replace("::ffff:", "")
?? "unknown"; ^
SyntaxError: Unexpected token '.' at Loader.moduleStrategy
(internal/modules/esm/translators.js:133:18) at async link
(internal/modules/esm/module_job.js:42:21)
We had the same issue after installing node on the Debian Server but after updating it with nvm install 19.4.0it fixed the problem
The command serve -s build did work then on the server but it does not work in the pipeline.
We are discussing the possibility that the container is using is his own environment but we are not sure with that assumption.
Can some help and explain the problem.
The error comes from the "new" optional chaining javascript feature ?. which is supported from v14. However default debian 11 node version is v12. Thus you should update node to newest version
sudo apt remove node npm nodejs #remove them old node/npm
sudo snap install node --classic #see https://snapcraft.io/node
or use instead nvm like you have done; to install specific node version

How to install nodejs to install a npm package in a gitlab job?

.deploy: &deploy
before_script:
- apt-get update -y
script:
- cd source/
- npm install multi-file-swagger
- multi-file-swagger -o yaml temp.yml > swagger.yml
I want to install multi-file-swagger package to compile the temp.yml( which has been split into multiple files) into the swagger.yml. So before using npm I need to install nodejs. How can I do that?
As the image is Debian-based, you should be able to install the source repo from Node and install the package from there. The relevant section of your Gitlab file would look like this:
.deploy: &deploy
before_script:
- apt-get update -y
script:
- curl -sL https://deb.nodesource.com/setup_17.x | bash
- apt-get install nodejs -yq
- cd source/
- npm install multi-file-swagger
- multi-file-swagger -o yaml temp.yml > swagger.yml
Please note that these additional steps will add a significant amount of time to your build process. If you are executing them more frequently, consider creating your own build image derived from the one you’re using now, and adding these steps into the image itself.

Cannot get GitLab CI to build my Middleman/Gulp project

I am using Gitlab-CI to build my Middleman application which also uses some node stuff for the front end (Gulp).
Here is my .gitlab-ci.yml (mostly copied from here):
image: ruby:2.3
cache:
paths:
- vendor
- node_modules
before_script:
- apt-get update -yqqq
- apt-get install -y npm
- ln -s /usr/bin/nodejs /usr/bin/node
- npm install
- bundle install --path vendor
test:
script:
- bundle exec middleman build
except:
- master
pages:
script:
- bundle exec middleman build
artifacts:
paths:
- public
only:
- master
Everything goes alright apart from the vital problem that it seems to be using an old version of node when it's npm installing. I'm getting lots of this:
npm WARN engine gulp-babel#7.0.0: wanted: {"node":">=4"} (current: {"node":"0.10.29","npm":"1.4.21"})
Before finally failing on the "const path" SyntaxError.
I included a line to symlink the new nodejs with the old name (- ln -s /usr/bin/nodejs /usr/bin/node) but it seems to have no effect...?
Been banging my head for long enough, there's got to be someone out there who has made this work?
Debian Jessie ships with a fixed NodeJs major version, follow NodeSource instructions to install a specific version, this would fit in your gitlab-ci.yml like this (you probably need to install curl first since its not installed in the ruby:2.3 image):
before_script:
- apt-get update -q && apt-get -qqy install curl
- curl -sL https://deb.nodesource.com/setup_9.x | bash -
- apt-get update -q && apt-get -qqy install nodejs npm
- ln -s /usr/bin/nodejs /usr/bin/node
- npm install
- bundle install --path vendor

gitlab.com CI - build a NodeJS app using docker in docker

I'm currently facing a problem with the gitlab.com shared-runners. What I'm trying to archieve in my pipeline is:
- NPM install and using grunt to make some uncss, minimize and compress tasks
- Cleaning up
- Building a docker container with the app included
- Moving the container to gitlab registry
Unfortunateley I don't get it running since a long time! I tried a lot of different gitlab.ci configs - without success. The problem is, that I have to use the "image: docker:latest" to have all the docker-tools running. But then I don't have node and grunt installed in the container.
Also the other way around is not working. I was trying to use image: centos:latest and install docker manually - but this is also not working as I always just get a Failed to get D-Bus connection: Operation not permitted
Does anyone has some more experience on the gitlab-ci using docker build commands in a docker shared runner?
Any help is highly appreciated!!
Thank you
Jannik
Gitlab can be a bit tricky :) I dont have an example based on CentOS, but I have one based on Ubuntu if that helps you. Here is some copy paste of a working gitlab pipeline of mine which uses gulp (you should be easily able to adjust it to work with your grunt).
The .gitlab-ci.yml looks like this (adjust the CONTAINER... variables at the beginning):
variables:
CONTAINER_TEST_IMAGE: registry.gitlab.com/psono/psono-client:$CI_BUILD_REF_NAME
CONTAINER_RELEASE_IMAGE: registry.gitlab.com/psono/psono-client:latest
stages:
- build
docker-image:
stage: build
image: ubuntu:16.04
services:
- docker:dind
variables:
DOCKER_HOST: 'tcp://docker:2375'
script:
- sh ./var/build-ubuntu.sh
- docker info
- docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" registry.gitlab.com
- docker build -t $CONTAINER_TEST_IMAGE .
- docker push $CONTAINER_TEST_IMAGE
in addition i have a this "./var/build-ubuntu.sh" which you can adjust a bit according to your needs, replace some ubuntu dependencies or switch gulp for grunt as needed:
#!/usr/bin/env bash
apt-get update && \
apt-get install -y libfontconfig zip nodejs npm git apt-transport-https ca-certificates curl openssl software-properties-common && \
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - && \
add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable" && \
apt-get update && \
apt-get install -y docker-ce && \
ln -s /usr/bin/nodejs /usr/bin/node && \
npm install && \
node --version && \
npm --version

AWS Elasticbeanstalk install node and npm in PHP instance type

I've got a PHP application that uses gulp to build the production files, I'm using elasticbeanstalk to deploy my app to and AWS EC2 instance and I want to install node and npm to run gulp during the deploy, my current configuration is:
01-installPackages.config inside .ebextenstions folder
commands:
01updateComposer:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update
02installNode:
command:
# run this command from /tmp directory
cwd: /tmp
# don't run the command if node is already installed (file /usr/bin/node exists)
test: '[ ! -f /usr/bin/node ] && echo "node not installed"'
# install from epel repository
# flag -y for no-interaction installation
command: 'sudo yum install -y nodejs --enablerepo=epel'
the node commands fails but if I ssh in and i run that command node is installed
any idea of what's wrong with my configuration?
Thanks!
The current answer for this is the following:
commands:
01enable_epel:
command: sudo amazon-linux-extras install epel
02npm_install:
command: sudo yum -y --enablerepo=epel install nodejs npm
using the extras library documented in AWS docs
You should be able to use the environment configuration options for this rather than writing a bash file yourself. You can use the packages key to download and install prepackaged applications and components.
This will allow you do something like this:
packages:
yum:
nodejs: []
npm: []
And the packages you require will be installed.
Note: This has been deprecated in Amazon Linux 2.

Resources