Node.Js Express CRUD template

This post is a guidance for very beginners about a Node.js express code with mysql2 which shows some examples for CRUD in Node.js

//npm init
//npm install nodemon, mysql2, express, cors


const express = require('express');
const mysql = require('mysql2/promise');
const cors = require('cors');

const app = express();

app.use(cors());
app.use(express.json());

const dbPool = mysql.createPool({
    host: 'localhost',     
    user: 'root',         
    password:  '',
    database: 'database_name', 
    port: 3306,         
});


app.get('/', (req, res) => {
  res.send('Backend running');
});



// -------------- CRUD --------------

// CREATE
app.post('/api/users', async (req, res) => {
    try {
        const { name, email } = req.body;

        if (!name || !email) {
            return res.status(400).json({ error: 'Error for the frontend (name or email missing)' });
        }

        const sql = "INSERT INTO users (name, email) VALUES (?, ?)";
        const [result] = await dbPool.query(sql, [name, email]);
        res.status(201).json({ message: "Response for the frontend", id: result.insertId });
    } catch (error) {
        console.error("Error for the backend ", error);
        res.status(500).json({ error: 'Error for the frontend' });
    }
});

// READ 
app.get('/api/users', async (req, res) => {
    try {
        const sql = "SELECT id, name, email FROM users";
        const [result] = await dbPool.query(sqlQuery);
        res.json(result);
    } catch (error) {
        console.error("Error for the backend ", error);
        res.status(500).json({ error: 'Error for the frontend' });
    }
});


// UPDATE
app.patch('/api/users/:id', async (req, res) => {
    try {
        const { id } = req.params;
        const { name, email } = req.body;

        if (!name || !email) {
             return res.status(400).json({ error: 'Error for the frontend (name or email missing)' });
        }

        const sql = "UPDATE users SET name = ?, email = ? WHERE id = ?";
        const [result] = await dbPool.query(sql, [name, email, id]);

         res.status(201).json({ message: "Response for the frontend", id: result.insertId });
    } catch (error) {
        console.error("Error for the backend ", error);
        res.status(500).json({ error: 'Error for the frontend' });
    }
});

// DELETE 
app.delete('/api/users/:id', async (req, res) => {
    try {
        const { id } = req.params;
        const sql = "DELETE FROM users WHERE id = ?";
        const [result] = await dbPool.query(sql, [id]);

       res.status(201).json({ message: "Response for the frontend", id: result.insertId });
    } catch (error) {
        console.error("Error for the backend ", error);
        res.status(500).json({ error: 'Error for the frontend' });
    }
});

const PORT = 3001;

app.listen(PORT, () => {
  console.log('Server is running on: http://localhost:'+PORT);
});

Lint Your Phishing Templates Like You Lint Your Code

You spent two hours crafting a convincing IT helpdesk pretext. You ship the campaign. The click rate is 2%.

It’s not because employees got more savvy. It’s because half your emails landed in spam, your tracking pixel was broken so you never saw the opens, and your {{.FirstName}} was actually {{.first_name}} and rendered as a literal string in every recipient’s inbox.

I built @hailbytes/phishing-template-linter after the third campaign in a row where this happened.

Lint a directory of templates

npx @hailbytes/phishing-template-linter ./templates/

You get a per-template report of:

  • Broken or unknown merge tags
  • Missing tracking pixel / link rewrite hooks
  • Spam-trigger phrases (the obvious ones, but also Gmail’s newer heuristics)
  • Deliverability red flags (mismatched display names, suspicious from-domain handling, bare URLs in plaintext)
  • Missing or malformed HTML/text alternatives

Use it programmatically

import { lint } from '@hailbytes/phishing-template-linter';

const result = lint(templateHtml);

// Errors fail the campaign launch; warnings get reviewed by a human
if (result.errors.length > 0) process.exit(1);

Wire it into CI

npx @hailbytes/phishing-template-linter ./templates/ --format=json > report.json

Drop that into your campaign-management pipeline and your phishing sims get the same pre-flight guardrails your production code already has.

It’s GoPhish-format aware, so the merge-tag rules know about {{.FirstName}}, {{.URL}}, {{.TrackingURL}}, and the rest of the GoPhish template grammar.

npm install @hailbytes/phishing-template-linter

Source: github.com/hailbytes/phishing-template-linter — MIT licensed. Built as a companion to the HailBytes SAT platform but works standalone with any GoPhish-format templates.