Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
larvitbase
Advanced tools
A scaled down version of Express. It is as micro as micro can be, it only runs an array of middlewere functions, nothing more.
npm i larvitbase
In case you're building a full webb system, you probably want to go directly to larvitbase-www or if you're building an API, you might want to look at larvitbase-api. Both of these is larvitbase plus some middlewares to handle the basics of each scenario.
This will create a http server on port 8001 that will print "Hello world" whatever you send in.
const App = require('larvitbase');
let app = new App({
'httpOptions': 8001, // Listening port
'middlewares': [
function (req, res) {
res.end('Hello world');
}
]
});
app.start(function (err) {
if (err) throw err;
});
Routing is how to match a special URL to a specific piece of code or pieces of code.
There is no built in routing, but it is easy to make your own or use the more fully fledged larvitrouter.
Something like this:
const App = require('larvitbase');
let app;
function router(req, res, cb) {
if (req.url === '/') {
req.controller = controllerOne;
} else if (req.url === '/foo') {
req.controller = controllerTwo;
} else {
req.controller = notFound;
}
cb();
}
function runController(req, res, cb) {
req.controller(req, res, cb);
}
function controllerOne(req, res, cb) {
res.end('This is controllerOne! Hepp!');
}
function controllerTwo(req, res, cb) {
res.end('This is the second controller function! Hola!');
}
function notFound(req, res, cb) {
res.statusCode = 404;
res.end('The URL matched no controller. Ledsen i ögat. :(');
}
app = new App({
'httpOptions': 8001, // Listening port
'middlewares': [
router, // First run the router
runController // Then run the routed controller
]
});
app.start(function (err) {
if (err) throw err;
});
From the path of your application, type:
node ./index.js
Then go to a browser and go to http://localhost:8001 and you should see "This is controllerOne! Hepp!". Test the URL:s /foo and /something as well and see what happends.
For a bit larger application, it is often desireble with a more competent router. For this we have larvitrouter. It will resolve paths to controller files as well as template files and static files. See the documentation for an in depth guide. Here follows a small example:
First install it:
npm i larvitrouter
const Router = require('larvitrouter'),
router = new Router(),
App = require('larvitbase');
let app;
function runRouter(req, res, cb) {
router.resolve(req.url, function (err, result) {
req.routeResult = result; // Store the route result on the request so we can access it from elsewhere
cb(err);
});
}
function runController(req, res, cb) {
// A controller file was found!
if (req.routeResult.controllerFullPath) {
const controller = require(req.routeResult.controllerFullPath);
controller(req, res, cb);
// No controller file was found
} else {
res.statusCode = 404;
return res.end('Not found');
}
}
app = new App({
'httpOptions': 8001, // Listening port
'middlewares': [
runRouter,
runController
]
});
app.start(function (err) {
if (err) throw err;
});
To make the URL /url work on our application, we create this file and save it like this:
'use strict';
exports = module.exports = function controllerFoo(req, res, cb) {
res.end('Foo custom page');
cb();
}
The default controller is a bit special. It will match both / and /default.
'use strict';
exports = module.exports = function controllerFoo(req, res, cb) {
res.end('Default page');
cb();
}
In this section we're going to implement EJS template engine.
First install dependencies:
npm i larvitbase ejs
const ejs = require('ejs'),
App = require('larvitbase');
let app;
function controller(req, res, cb) {
// Set some different data depending on URL
res.data = {};
if (req.url === '/') {
res.data.someText = 'First page';
} else if (req.url === '/foo') {
res.data.someText = 'A sub page';
} else {
res.data.someText = 'No page';
}
cb();
}
function renderTemplate(req, res, cb) {
res.body = ejs.render('<html><body><h1><%= someText %></h1></body></html>', res.data);
res.end(res.body);
cb();
}
app = new App({
'httpOptions': 8001, // Listening port
'middlewares': [
controller,
renderTemplate
]
});
app.start(function (err) {
if (err) throw err;
});
To parse forms (and file uploads, url parameters and more) we can use larvitreqparser.
Install dependencies:
npm i larvitbase larvitreqparser
const ReqParser = require('larvitreqparser'),
reqParser = new ReqParser(),
App = require('larvitbase');
let app;
function controller(req, res, cb) {
res.body = '<html><body><form method="post">';
res.body += '<p>Write something: <input name="foo" /></p>';
if (req.formFields && req.formFields.foo) {
res.body += '<p>Something was written: "' + req.formFields.foo + '"</p>'
}
res.body += '<p><button type="submit">Click me</button></p>';
res.body += '</body></html>';
res.end(res.body);
cb();
}
app = new App({
'httpOptions': 8001, // Listening port
'middlewares': [
reqParser.parse.bind(reqParser),
controller
]
});
app.start(function (err) {
if (err) throw err;
});
To feed static files, we are going to use a router to determine if the URL points to a static file, and then send to stream the file to the client.
Install dependencies:
npm i larvitbase larvitrouter send
const Router = require('larvitrouter'),
router = new Router({'staticsPath': __dirname + '/public'}), // This is the default, but we're explicit here
send = require('send'),
App = require('larvitbase');
let app;
function runRouter(req, res, cb) {
router.resolve(req.url, function (err, result) {
req.routeResult = result;
cb(err);
});
}
function controller(req, res, cb) {
// Static file found! Stream it to the client.
if (req.routeResult.staticFullPath) {
send(req, req.routeResult.staticFullPath).pipe(res);
// End the controller here, nothing more should be sent to the client
return cb();
}
// What to show if no static file is found:
res.body = '<html><body><h1>Show a static file:</h1>';
res.body += '<p><a href="foo.txt">foo.txt</a></p>';
res.body += '</body></html>';
res.end(res.body);
cb();
}
app = new App({
'httpOptions': 8001, // Listening port
'middlewares': [
runRouter,
controller
]
});
app.start(function (err) {
if (err) throw err;
});
Just save a text file with whatever content in hour applications path + public/foo.txt
In case something goes wrong inside any of your middlewares and an error is returned, we need to handle that somehow. This is how:
const App = require('larvitbase');
let app;
app = new App({
'httpOptions': 8001, // Listening port
'middleware': [
function (req, res, cb) {
return cb(new Error('Something went wrong! :('))
}
]
});
// Handle errors in one of the middleweres during a request
app.on('error', function (err, req, res) {
res.statusCode = 500;
res.end('Internal server error: ' + err.message);
});
app.start(function (err) {
if (err) throw err;
});
The App takes a log option and it requires it to be an object with the following methods: silly(), debug(), verbose(), info(), warn() and error().
Historically we've used winston, but any object with the above methods will work fine. If no log instance is supplied, an instance of larvitutils Log will be used with level "info".
Log levels used:
Do not use in production
Middleware functions are compatible with Express, and follow the same principles:
Middleware functions can perform the following tasks:
FAQs
http application base framework
The npm package larvitbase receives a total of 361 weekly downloads. As such, larvitbase popularity was classified as not popular.
We found that larvitbase demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.