Welcome to FreeSoftwareServers Confluence Wiki

mkdir -p /opt/express/SERVERNAME/
cd /opt/express/SERVERNAME/
cat <<'EOF'>server.js
const express = require('express'); 
const request = require('request');

const config = {
    name: 'SERVERNAME',
    port: 3000,
    host: '0.0.0.0',
};

const app = express();

app.use(express.text({limit: '50mb'})) //For reading text/*
app.use(express.json()); //To support JSON-encoded bodies

//CORS
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS')
  res.header('Access-Control-Allow-Credentials', true)
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Methods, Credentials')
  next()
})

//////
app.get("/", (req, res) => {
  const url = 'http://192.168.1.151:9200/'
  request(url).pipe(res);
});
//////
//////
app.post('/dev', async function (req, res) {
    var body = req.body
    res.end(JSON.stringify(body))
});
//////
app.listen(config.port, config.host, (e)=> {
    if(e) {
        throw new Error('Internal Server Error');
    }
});

////
EOF
chmod +x server.js
cat <<'EOF'>docker-compose.yaml
version: '3'
services:
  api:
    container_name: SERVERNAME-api
    image: SERVERNAME
    build: .
    ports:
      - "4201:3000"
    volumes:
      - type: bind
        source: /opt/express/SERVERNAME/
        target: /usr/src/app

EOF
chmod +x docker-compose.yaml
cat <<'EOF'>Dockerfile

FROM node:lts-alpine

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY . .

RUN npm config set strict-ssl false
RUN npm install

EXPOSE 3000

#CMD ["npm", "run", "start"]
CMD ["npm", "run", "SERVERNAME"]
EOF
chmod +x Dockerfile
cat <<'EOF'>package.json
{
  "name": "SERVERNAME",
  "version": "1.0.0",
  "description": "SERVERNAME",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "SERVERNAME": "nodemon server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1",
    "request": "^2.88.2",
    "xmlserializer": "^0.6.1",
    "jsdom": "^19.0.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.16"
  }
}
EOF
chmod +x package.json
#Make sure latest NodeJS/NPM Installed, dont' use APT
npm install
cat <<'EOF'>SERVERNAME.service.setup.sh
cat << EOL >/lib/systemd/system/SERVERNAME.service
[Unit]
Description=SERVERNAME
Requires=docker.service network-online.target

[Service]

WorkingDirectory=/opt/express/SERVERNAME
ExecStartPre=
Restart=on-abnormal
ExecStart=/usr/bin/docker-compose --project-name SERVERNAME --project-directory /opt/express/SERVERNAME -f /opt/express/SERVERNAME/docker-compose.yaml up --force-recreate
ExecStop=/usr/bin/docker-compose --project-name SERVERNAME --project-directory /opt/express/SERVERNAME -f /opt/express/SERVERNAME/docker-compose.yaml stop

[Install]
WantedBy=multi-user.target
EOL
systemctl enable SERVERNAME
systemctl restart SERVERNAME
systemctl status SERVERNAME
EOF
chmod +x SERVERNAME.service.setup.sh
  • No labels