Complete API reference for the Newgate multi-format backend framework.
The main application class for creating an Newgate server.
import App from 'newgatejs';
const app = new App();Register a GET route.
app.get('/users/:id', (req, res) => {
res.json({ userId: req.params.id });
});Register a POST route.
app.post('/users', (req, res) => {
res.status(201).json({ created: true });
});Register a PUT route.
app.put('/users/:id', (req, res) => {
res.json({ updated: true });
});Register a DELETE route.
app.delete('/users/:id', (req, res) => {
res.json({ deleted: true });
});Register a PATCH route.
app.patch('/users/:id', (req, res) => {
res.json({ patched: true });
});Register global middleware.
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});Path-specific middleware:
app.use('/admin', (req, res, next) => {
// Only runs for /admin/* routes
next();
});Register error handling middleware.
app.useError((err, req, res, next) => {
res.status(500).json({ error: err.message });
});Configure CORS settings.
app.cors({
origin: 'https://example.com',
methods: ['GET', 'POST', 'PUT', 'DELETE'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
maxAge: 86400
});Register a shutdown hook.
app.onShutdown(async () => {
// Cleanup code
console.log('Shutting down...');
});Start the server.
app.listen(3000, () => {
console.log('Server running on port 3000');
});Gracefully shutdown the server.
await app.shutdown();The request object with parsed body and parameters.
Parsed request body based on content type.
app.post('/data', (req, res) => {
console.log(req.body); // { name: 'John', age: 30 }
});Type of parsed body: 'json', 'csv', 'xml', 'yaml', 'formdata', 'binary', or null.
app.post('/data', (req, res) => {
if (req.bodyType === 'json') {
// Handle JSON
}
});Route parameters extracted from URL.
app.get('/users/:id/posts/:postId', (req, res) => {
console.log(req.params); // { id: '123', postId: '456' }
});Query string parameters.
app.get('/search', (req, res) => {
console.log(req.query); // { q: 'test', limit: '10' }
});Enhanced response object with helper methods.
Set HTTP status code. Returns response for chaining.
res.status(201).json({ created: true });Set response header. Returns response for chaining.
res.set('X-Custom-Header', 'value').json({ ok: true });Send JSON response.
res.json({ message: 'Hello' });Send plain text or buffer response.
res.send('Hello World');Send CSV response.
res.csv('name,age\nJohn,30\nJane,25');Send XML response.
res.xml('<?xml version="1.0"?><root><name>test</name></root>');Send YAML response.
res.yaml('name: test\nvalue: 123');Send binary file response.
const buffer = fs.readFileSync('image.png');
res.file(buffer, 'image/png');Send error response.
res.error({
message: 'Not found',
code: 404,
details: { resource: 'user' }
});Pipe a readable stream to response.
const stream = fs.createReadStream('large-file.txt');
res.stream(stream);Send file as download.
res.download('/path/to/file.pdf', 'document.pdf');Extract dynamic values from URL paths.
app.get('/users/:id', (req, res) => {
res.json({ userId: req.params.id });
});
// GET /users/123 → { userId: '123' }Match any path segment.
app.get('/files/*', (req, res) => {
res.json({ path: req.url });
});
// GET /files/documents/file.txt → matchesAccess query parameters.
app.get('/search', (req, res) => {
const { q, limit } = req.query;
res.json({ query: q, limit });
});
// GET /search?q=test&limit=10 → { query: 'test', limit: '10' }Combine multiple route parameters.
app.get('/users/:userId/posts/:postId', (req, res) => {
res.json(req.params);
});
// GET /users/42/posts/99 → { userId: '42', postId: '99' }Runs for all requests.
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});Runs only for matching paths.
app.use('/admin', (req, res, next) => {
// Verify admin access
next();
});Runs for specific routes.
app.get(
'/protected',
(req, res, next) => {
// Authentication middleware
next();
},
(req, res) => {
res.json({ protected: true });
}
);Support for async operations.
app.use(async (req, res, next) => {
await database.connect();
next();
});Handle errors from routes and middleware.
app.useError((err, req, res, next) => {
console.error(err);
res.status(500).json({ error: err.message });
});// Automatic parsing for Content-Type: application/json
app.post('/data', (req, res) => {
console.log(req.body); // Parsed JSON object
});Error handling:
try {
const data = parseJSON(buffer);
} catch (err) {
console.error(err.message); // "Invalid JSON: ..."
console.error(err.statusCode); // 400
}// Automatic parsing for Content-Type: text/csv
app.post('/data', (req, res) => {
console.log(req.body); // Array of objects
});Options:
const options = {
headers: true, // Use first row as headers (default: true)
delimiter: ',', // Field delimiter (default: ',')
skipEmptyLines: true, // Skip empty lines (default: true)
schema: { // Optional schema validation
age: (val) => !isNaN(parseInt(val))
}
};// Automatic parsing for Content-Type: application/xml
app.post('/data', (req, res) => {
console.log(req.body); // Parsed XML object
});Options:
const options = {
safeMode: true, // Disable external entities (default: true)
strict: true // Strict parsing mode (default: true)
};// Automatic parsing for Content-Type: application/x-yaml
app.post('/data', (req, res) => {
console.log(req.body); // Parsed YAML object
});Options:
const options = {
multiDoc: false // Parse multiple documents (default: false)
};// Automatic parsing for Content-Type: multipart/form-data
app.post('/upload', (req, res) => {
console.log(req.body.fields); // Form fields
console.log(req.body.files); // Uploaded files
});Options:
const options = {
fileSizeLimit: 10 * 1024 * 1024, // 10MB (default)
memoryLimit: 50 * 1024 * 1024, // 50MB (default)
fileCountLimit: 10 // Max files (default)
};File structure:
{
fields: {
name: 'John',
email: 'john@example.com'
},
files: {
avatar: {
filename: 'avatar.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
size: 12345,
buffer: <Buffer>
}
}
}Parse and extract URL components.
import parseURL from 'newgatejs/utils/urlParser.js';
const parsed = parseURL('/search?q=test#results');
// {
// protocol: 'http',
// hostname: 'localhost',
// port: 80,
// pathname: '/search',
// search: '?q=test',
// hash: '#results',
// query: { q: 'test' },
// href: 'http://localhost/search?q=test#results',
// origin: 'http://localhost'
// }Recursively merge objects.
import deepMerge from 'newgatejs/utils/deepMerge.js';
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { b: { d: 3 }, e: 4 };
const merged = deepMerge(obj1, obj2);
// { a: 1, b: { c: 2, d: 3 }, e: 4 }import App from 'newgatejs';
const app = new App();
app.get('/', (req, res) => {
res.json({ message: 'Hello World' });
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});import App from 'newgatejs';
const app = new App();
const users = [];
app.get('/users', (req, res) => {
res.json(users);
});
app.post('/users', (req, res) => {
const user = req.body;
users.push(user);
res.status(201).json(user);
});
app.get('/users/:id', (req, res) => {
const user = users[req.params.id];
if (user) {
res.json(user);
} else {
res.status(404).json({ error: 'User not found' });
}
});
app.listen(3000);import App from 'newgatejs';
const app = new App();
app.post('/data', (req, res) => {
switch (req.bodyType) {
case 'json':
res.json({ received: 'json', data: req.body });
break;
case 'csv':
res.json({ received: 'csv', records: req.body.length });
break;
case 'xml':
res.json({ received: 'xml', root: Object.keys(req.body)[0] });
break;
default:
res.status(400).json({ error: 'Unsupported format' });
}
});
app.listen(3000);import App from 'newgatejs';
const app = new App();
// Logging middleware
app.use((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
next();
});
// Authentication middleware
app.use('/admin', (req, res, next) => {
const token = req.headers.authorization;
if (token === 'Bearer secret') {
next();
} else {
res.status(401).json({ error: 'Unauthorized' });
}
});
app.get('/admin/stats', (req, res) => {
res.json({ users: 100, posts: 500 });
});
app.listen(3000);