Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.git
.DS_Store
npm-debug.log
yarn-error.log
46 changes: 46 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
validate:
name: Validate Source Code
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v5

- name: Setup node
uses: actions/setup-node@v5
with:
node-version: '24'
cache: yarn
cache-dependency-path: '**/yarn.lock'

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn test

- name: Build Docker image
run: docker compose build

- name : Run Docker container
run: docker compose up -d

- name: Notify on success
if: success()
run: echo "CI Pipeline passed successfully!"

- name: Notify on failure
if: failure()
run: echo "CI Pipeline failed! Please check the logs."
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM node:24-alpine

WORKDIR /app

ENV NODE_ENV=production
ENV PORT=3001

COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --production=true

COPY . .

EXPOSE 3001

CMD ["yarn", "start"]
20 changes: 15 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ const port = process.env.PORT || 3001;

app.get("/", (req, res) => res.type('html').send(html));

const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`));
function startServer() {
const server = app.listen(port, () => console.log(`Example app listening on port ${port}!`));

server.keepAliveTimeout = 120 * 1000;
server.headersTimeout = 120 * 1000;
server.keepAliveTimeout = 120 * 1000;
server.headersTimeout = 120 * 1000;

return server;
}

const html = `
<!DOCTYPE html>
<html>
<head>
<title>Hello from Render!</title>
<title>Hello from 23521574 - Ngô Quang Tiến!</title>
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.5.1/dist/confetti.browser.min.js"></script>
<script>
setTimeout(() => {
Expand Down Expand Up @@ -54,8 +58,14 @@ const html = `
</head>
<body>
<section>
Hello from Render!
Hello from 23521574 - Ngô Quang Tiến!
</section>
</body>
</html>
`

if (require.main === module) {
startServer();
}

module.exports = app;
52 changes: 52 additions & 0 deletions app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const assert = require("node:assert/strict");
const { after, before, describe, it } = require("node:test");

const app = require("./app");

let server;
let baseUrl;

function request(path) {
return fetch(`${baseUrl}${path}`);
}

describe("Express app", () => {
before(async () => {
server = app.listen(0);

await new Promise((resolve) => {
server.once("listening", resolve);
});

const { port } = server.address();
baseUrl = `http://127.0.0.1:${port}`;
});

after(async () => {
await new Promise((resolve, reject) => {
server.close((error) => {
if (error) {
reject(error);
return;
}

resolve();
});
});
});

it("returns the home page", async () => {
const response = await request("/");
const body = await response.text();

assert.equal(response.status, 200);
assert.match(response.headers.get("content-type"), /^text\/html/);
assert.match(body, /Hello from 23521574/);
});

it("returns 404 for unknown routes", async () => {
const response = await request("/not-found");

assert.equal(response.status, 404);
});
});
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
web:
build: .
ports:
- "3001:3001"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"license": "MIT",
"private": false,
"scripts": {
"start": "node app.js"
"start": "node app.js",
"test": "node --test"
},
"dependencies": {
"express": "^5.0.0"
Expand Down