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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay