diamondjs
v0.1.1
Published
Minimalistic web framework for Node.js
Downloads
4
Readme
About
Diamond JS is Express inspired framework, made for learning purposes.
Installation
npm install diamondjs
Basic example
const Diamond = require('diamondjs');
const server = new Diamond();
server.get('/', (req, res) => {
res.write('Hello world!');
});
server.start(8080);
Docs
Module initialization
const Diamond = require('diamondjs');
const server = new Diamond();
Starting server
const port = 8080;
server.start(port);
Middleware
Custom
server.use((req, res, next) => {
console.log('Middleware 1 called');
next();
});
Modules
const morgan = require('morgan');
server.use(morgan('dev'));
Routing
Supported methods are POST GET PUT PATCH DELETE
Basic
server.route('GET', '/news', (req, res) => {
res.write('Hello news!');
});
server.route('POST', ...);
server.route('PUT', ...);
server.route('PATCH', ...);
server.route('DELETE', ...);
Basic short
server.get('/admin', (req, res) => {
res.write('Hello admin!');
});
server.post(...);
server.put(...);
server.patch(...);
server.delete(...);
Controller route
app.js
// Set controllers directory (optional, default is controllers)
// server.setControllersDir('example/controllers');
server.route('GET', '/', 'MainController@index');
server.route('GET', '/home', 'MainController@home');
controllers/MainController.js
exports.index = (req, res) => {
res.write('Index');
};
exports.home = (req, res) => {
res.write('Home');
};
View engine
Only pug
view engine is supported for now.
Setup
// Set controllers directory (optional, default is views)
// server.setViewsDir('example/views');
server.useViewEngine('pug');
Render
// This will render views/news.pug
server.route('GET', '/news', (req, res) => {
res.render('news');
});