feat: INS hotel technology landing page with lead capture #1
No files matched your search
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"name": "ins-landing-page",
|
||||||
|
"private": true,
|
||||||
|
"engines": { "node": ">=22.13" },
|
||||||
|
"scripts": {
|
||||||
|
"start": "node server.js",
|
||||||
|
"test": "node --test"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
<!doctype html><title>INS</title>
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
const http = require('node:http');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const path = require('node:path');
|
||||||
|
const { DatabaseSync } = require('node:sqlite');
|
||||||
|
|
||||||
|
const MAX_BODY = 10 * 1024;
|
||||||
|
const REQUIRED = ['name', 'email', 'phone', 'company'];
|
||||||
|
const OPTIONAL = ['industry', 'message', 'utm_source', 'utm_medium', 'utm_campaign'];
|
||||||
|
const MAX_LEN = { message: 2000 };
|
||||||
|
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||||
|
const INDEX = path.join(__dirname, 'public', 'index.html');
|
||||||
|
const STATIC = { '/logo.png': ['logo.png', 'image/png'] };
|
||||||
|
|
||||||
|
function openDb(dbPath) {
|
||||||
|
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
||||||
|
const db = new DatabaseSync(dbPath);
|
||||||
|
db.exec(`CREATE TABLE IF NOT EXISTS leads (
|
||||||
|
id INTEGER PRIMARY KEY,
|
||||||
|
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
name TEXT NOT NULL, email TEXT NOT NULL, phone TEXT NOT NULL, company TEXT NOT NULL,
|
||||||
|
industry TEXT, message TEXT, referrer TEXT,
|
||||||
|
utm_source TEXT, utm_medium TEXT, utm_campaign TEXT
|
||||||
|
)`);
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validate(lead) {
|
||||||
|
for (const f of REQUIRED) {
|
||||||
|
if (typeof lead[f] !== 'string' || !lead[f].trim()) return `${f} is required`;
|
||||||
|
}
|
||||||
|
for (const f of [...REQUIRED, ...OPTIONAL]) {
|
||||||
|
const v = lead[f];
|
||||||
|
if (v != null && typeof v !== 'string') return `${f} must be text`;
|
||||||
|
if (v && v.length > (MAX_LEN[f] || 200)) return `${f} is too long`;
|
||||||
|
}
|
||||||
|
if (!EMAIL.test(lead.email.trim())) return 'email is invalid';
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function send(res, status, body) {
|
||||||
|
res.writeHead(status, { 'content-type': 'application/json' });
|
||||||
|
res.end(JSON.stringify(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
function readBody(req) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let size = 0;
|
||||||
|
const chunks = [];
|
||||||
|
req.on('data', (c) => {
|
||||||
|
size += c.length;
|
||||||
|
// keep draining (not destroy) so the 413 reaches the client; the ingress body limit (nginx default 1m) caps abuse
|
||||||
|
if (size > MAX_BODY) reject(Object.assign(new Error('too large'), { status: 413 }));
|
||||||
|
else chunks.push(c);
|
||||||
|
});
|
||||||
|
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
||||||
|
req.on('error', reject);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createServer(dbPath) {
|
||||||
|
const db = openDb(dbPath);
|
||||||
|
const insert = db.prepare(`INSERT INTO leads
|
||||||
|
(name, email, phone, company, industry, message, referrer, utm_source, utm_medium, utm_campaign)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
||||||
|
|
||||||
|
const server = http.createServer(async (req, res) => {
|
||||||
|
const url = req.url.split('?')[0];
|
||||||
|
|
||||||
|
if (req.method === 'GET' && url === '/') {
|
||||||
|
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
||||||
|
return fs.createReadStream(INDEX).pipe(res);
|
||||||
|
}
|
||||||
|
if (req.method === 'GET' && STATIC[url]) {
|
||||||
|
const [file, type] = STATIC[url];
|
||||||
|
res.writeHead(200, { 'content-type': type, 'cache-control': 'public, max-age=86400' });
|
||||||
|
return fs.createReadStream(path.join(__dirname, 'public', file)).pipe(res);
|
||||||
|
}
|
||||||
|
if (req.method === 'POST' && url === '/api/leads') {
|
||||||
|
let lead;
|
||||||
|
try {
|
||||||
|
lead = JSON.parse(await readBody(req));
|
||||||
|
} catch (e) {
|
||||||
|
return send(res, e.status || 400, { error: e.status ? 'Request too large' : 'Invalid request' });
|
||||||
|
}
|
||||||
|
if (!lead || typeof lead !== 'object') return send(res, 400, { error: 'Invalid request' });
|
||||||
|
if (lead.website) return send(res, 200, { ok: true }); // honeypot: pretend success, store nothing
|
||||||
|
const error = validate(lead);
|
||||||
|
if (error) return send(res, 400, { error });
|
||||||
|
try {
|
||||||
|
const t = (f) => (lead[f] ? lead[f].trim() : null);
|
||||||
|
insert.run(t('name'), t('email'), t('phone'), t('company'), t('industry'), t('message'),
|
||||||
|
(req.headers.referer || '').slice(0, 500) || null, t('utm_source'), t('utm_medium'), t('utm_campaign'));
|
||||||
|
} catch (e) {
|
||||||
|
console.error('lead insert failed:', e.message);
|
||||||
|
return send(res, 500, { error: 'Could not save, please try again' });
|
||||||
|
}
|
||||||
|
return send(res, 201, { ok: true });
|
||||||
|
}
|
||||||
|
send(res, 404, { error: 'Not found' });
|
||||||
|
});
|
||||||
|
server.on('close', () => db.close());
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { createServer };
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
const port = Number(process.env.PORT) || 3000;
|
||||||
|
createServer(process.env.DB_PATH || path.join(__dirname, 'data', 'leads.db'))
|
||||||
|
.listen(port, () => console.log(`listening on :${port}`));
|
||||||
|
}
|
||||||
@@ -0,0 +1,94 @@
|
|||||||
|
const { test, before, after } = require('node:test');
|
||||||
|
const assert = require('node:assert');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
const os = require('node:os');
|
||||||
|
const path = require('node:path');
|
||||||
|
const { DatabaseSync } = require('node:sqlite');
|
||||||
|
const { createServer } = require('./server');
|
||||||
|
|
||||||
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ins-leads-'));
|
||||||
|
const dbPath = path.join(dir, 'leads.db');
|
||||||
|
let server, base;
|
||||||
|
|
||||||
|
before(async () => {
|
||||||
|
server = createServer(dbPath);
|
||||||
|
await new Promise((r) => server.listen(0, r));
|
||||||
|
base = `http://127.0.0.1:${server.address().port}`;
|
||||||
|
});
|
||||||
|
after(() => {
|
||||||
|
server.close();
|
||||||
|
fs.rmSync(dir, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
const valid = {
|
||||||
|
name: 'Aisyah Rahman',
|
||||||
|
email: 'aisyah@example.com',
|
||||||
|
phone: '+60 12-345 6789',
|
||||||
|
company: 'Hotel Seri Pinang',
|
||||||
|
industry: 'Hospitality',
|
||||||
|
message: 'Need guest Wi-Fi for 120 rooms',
|
||||||
|
utm_source: 'google',
|
||||||
|
};
|
||||||
|
|
||||||
|
const post = (body) =>
|
||||||
|
fetch(`${base}/api/leads`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'content-type': 'application/json', referer: 'https://ads.example.com/' },
|
||||||
|
body: typeof body === 'string' ? body : JSON.stringify(body),
|
||||||
|
});
|
||||||
|
|
||||||
|
const rows = () => {
|
||||||
|
const db = new DatabaseSync(dbPath);
|
||||||
|
const r = db.prepare('SELECT * FROM leads').all();
|
||||||
|
db.close();
|
||||||
|
return r;
|
||||||
|
};
|
||||||
|
|
||||||
|
test('serves the landing page', async () => {
|
||||||
|
const res = await fetch(`${base}/`);
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
assert.match(res.headers.get('content-type'), /text\/html/);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('valid lead is stored', async () => {
|
||||||
|
const res = await post(valid);
|
||||||
|
assert.strictEqual(res.status, 201);
|
||||||
|
const r = rows();
|
||||||
|
assert.strictEqual(r.length, 1);
|
||||||
|
assert.strictEqual(r[0].name, valid.name);
|
||||||
|
assert.strictEqual(r[0].company, valid.company);
|
||||||
|
assert.strictEqual(r[0].utm_source, 'google');
|
||||||
|
assert.strictEqual(r[0].referrer, 'https://ads.example.com/');
|
||||||
|
assert.ok(r[0].created_at);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('missing required field is rejected', async () => {
|
||||||
|
const before = rows().length;
|
||||||
|
const res = await post({ ...valid, phone: '' });
|
||||||
|
assert.strictEqual(res.status, 400);
|
||||||
|
assert.ok((await res.json()).error);
|
||||||
|
assert.strictEqual(rows().length, before);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('invalid email is rejected', async () => {
|
||||||
|
assert.strictEqual((await post({ ...valid, email: 'abc' })).status, 400);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('over-long field is rejected', async () => {
|
||||||
|
assert.strictEqual((await post({ ...valid, name: 'x'.repeat(201) })).status, 400);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('non-JSON body is rejected', async () => {
|
||||||
|
assert.strictEqual((await post('not json')).status, 400);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('oversized body is rejected', async () => {
|
||||||
|
assert.strictEqual((await post({ ...valid, message: 'x'.repeat(11000) })).status, 413);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('honeypot submission is accepted but not stored', async () => {
|
||||||
|
const before = rows().length;
|
||||||
|
const res = await post({ ...valid, website: 'http://spam.example' });
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
assert.strictEqual(rows().length, before);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user