DEV Community

Salman
Salman

Posted on

Melakukan pengujian Express.js menggunakan node-plug

Langkah pertama instalasi node-plug dan express.js

npm install node-plug 
Enter fullscreen mode Exit fullscreen mode
npm install express 
Enter fullscreen mode Exit fullscreen mode

Buat file pengujian misalnya test.js

import express from 'express' const app = express() const port = 3000 app.get('/', (req, res) => { res.send(message) }) const message = 'Halo dari Express dalam sebuah plugin!' // atau kirim sebagai JSON // const message = { message: 'Halo dari Express dalam sebuah plugin!' } export const t = { async run() { await console.log(message) }, } app.listen(port, () => { console.log(`Server berjalan di http://localhost:${port}`) }) 
Enter fullscreen mode Exit fullscreen mode

Setelah itu buat file, misalnya main.js atau index.js

import { addPlugin, runPlugin, test } from 'node-plug' import { t } from './test.js' // Menambahkan plugin addPlugin(t) // Menjalankan plugin runPlugin() // Melakukan pengujian test(['Halo dari Express dalam sebuah plugin!']) // Melakukan pengujian sebagai JSON /*test([ { message: 'Halo dari Express dalam sebuah plugin!', }, ])*/ 
Enter fullscreen mode Exit fullscreen mode

Output:

Image description

Image description

Output JSON:

Image description

Image description

Top comments (0)