Skip to content

Commit 08969e3

Browse files
committed
add not found dan update file upload
1 parent f3d8953 commit 08969e3

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

tutorial/nodejs/express/21-file-upload.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,36 @@ app.post('/file-upload', async (req, res) => {
5858
res.send(`Hello ${req.body.name} File ${textFile.name} uploaded!`);
5959
});
6060
```
61+
62+
### 🔥 Fullcode dengan Unit Test
63+
64+
Berikut contoh kode di atas dengan unit test:
65+
66+
```js
67+
const request = require('supertest');
68+
const express = require('express');
69+
const fileUpload = require('express-fileupload');
70+
71+
const app = express();
72+
app.use(express.json()); // menambahkan middleware untuk parsing JSON
73+
app.use(express.urlencoded({ extended: false })); // menambahkan middleware untuk parsing form data dengan extended: false, yang berarti hanya mendukung tipe data sederhana
74+
app.use(fileUpload()); // menambahkan middleware untuk handling file upload
75+
76+
app.post('/file-upload', async (req, res) => {
77+
const textFile = req.files.textFile;
78+
await textFile.mv(__dirname + "/upload/" + textFile.name);
79+
80+
res.send(`Hello ${req.body.name} File ${textFile.name} uploaded!`);
81+
});
82+
83+
// Unit Test
84+
test('Test Request File Upload /file-upload endpoint', async () => {
85+
const response = await request(app)
86+
.post('/file-upload')
87+
.set('Content-Type', 'multipart/form-data')
88+
.field('name', 'Ucup')
89+
.attach('textFile', __dirname + '/hello.txt');
90+
91+
expect(response.text).toBe('Hello Ucup File hello.txt uploaded!');
92+
});
93+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
sidebar_position: 22
3+
title: 'Not Found Handling'
4+
---
5+
6+
Saat pengguna melakukan permintaan ke URL yang tidak ada, maka secara default Express.js akan menampilkan halaman 404. Kita dapat membuat halaman 404 kita sendiri, dengan menambahkan middleware di posisi terakhir. Middleware ini akan diaktifkan jika tidak ada rute yang ditemukan untuk jalur rute yang diakses.
7+
8+
```js
9+
const express = require('express');
10+
11+
const app = express();
12+
13+
app.use((req, res, next) => {
14+
res.status(404).send('Halaman tidak ditemukan');
15+
});
16+
```
17+
18+
Untuk full code dengan unit test sebagai berikut:
19+
20+
```js
21+
const request = require('supertest');
22+
const express = require('express');
23+
24+
const app = express();
25+
26+
// endpoint yang ada
27+
app.get('/hello', (req, res) => {
28+
res.send('Hello World!');
29+
});
30+
31+
app.use((req, res, next) => {
32+
res.status(404).send('Halaman tidak ditemukan');
33+
});
34+
35+
// Unit Test
36+
test('Test Response /hello endpoint', async () => {
37+
const response = await request(app).get('/hello'); // endpoint ada
38+
expect(response.status).toBe(200);
39+
expect(response.text).toBe('Hello World!');
40+
});
41+
42+
test('Test Response Not Found', async () => {
43+
const response = await request(app).get('/halaman-ini-harusnya-tidak-ada'); // endpoint ini tidak ada
44+
expect(response.status).toBe(404);
45+
expect(response.text).toBe('Halaman tidak ditemukan');
46+
});
47+
```

0 commit comments

Comments
 (0)