Kenapa Express?

Awalnya mau deploy redirector ipang.my.id ke ipang.my.id gratisan di heroku. Deploy app nya sukses, gampang tanpa hambatan apapun, tapi ternyata kalau mau pake custom domain di heroku, kudu masukin kartu kredit..

Github/gitlab pages ga support nodejs. Nyobain beberapa PAAS, ribet setup nya karena harus di buat function serverless.

Jadinya tetep deploy di cloud sendiri, biar ga pusing kelamaan.


Membuat Aplikasi Redirector

  1. Buat 1 buat file index.js
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const host = '0.0.0.0';

app.get('*',function (req, res) {
    res.redirect('https://ipang.my.id' + req.url);
});

app.listen(port, host, function () {
  console.log(`Example app listening on port ${port}`)
})
  1. Inisialisasi
npm init
npm install express --save
  1. Jalankan node index.js

Setup Docker

  1. Buat file Dockerfile
FROM node:16-alpine
LABEL maintainer="ipang"
LABEL description="redirect using express"
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app/package.json

RUN npm install --production

COPY . /app

EXPOSE 3000

CMD ["node", "index.js"]
  1. Buat file docker-compose.yml
version: "3.3"

services:
   redirectdomain:
      container_name: redirect-ctr
      build: .
      restart: always
      ports:
         - 3000:3000
  1. Jalankan docker-compose build untuk membuat docker image lalu jalankan docker-compose up -d untuk menjalankan aplikasi

Catatan:

  1. Anda bisa clone semua file di atas di goblog-redirector github .

  2. Contohnya bisa langsung akses ipang.my.id maka akan langsung di redirect ke ipang.my.id

  3. Contoh redirect full path ipang.my.id - goofys alternatif s3fs


Referensi:

Bind nodejs port in heroku

Expressjs redirection


Jika kamu merasa tulisan ini bermanfaat & membantu kamu, kamu bisa berdonasi lewat saweria

If you feel this website help you, you can donate at saweria