Basic Site



A simple module that runs express with a common setup for a basic website.
Develop your express sites faster.
Useful if you tend to make a lot of apis or web apps that run on express.
Behind the sense, this module adds in middleware for basic security and compression.
- Also sets "trust proxy" to true for reverse proxies like nginx.
- Includes the ability to add any view engine you choose.
- Includes the optional default view engine (@aspiesoft/regve) pre setup if installed as a dependency.
- Runs the helmet module.
- Adds easy access to validator, and a function to sanitize variable types.
- Combines req.body and req.query into a single req.data (post data has priority over get data).
- In production (process.env.NODE_ENV === 'production'), forces ssl and verifies if the hostname is a FQDN with validator.
- Checks if the request is from localhost.
- Gets the geo IP country code with the geoip-country module.
- Runs a simple check to detect bots using the isbot-fast module (could be useful for SEO development).
- Simplifies the req.url to remove extra / at end, and removes query vars from the url (still accessible with req.query).
- Only allows GET and POST methods with "Access-Control-Allow-Methods".
- Limits the request size to 1mb.
- Adds a /ping url that runs before the view engine (simply returns "pong!") (useful for quickly checking if the server is online).
- You still have full access to the express module.
Installation
npm install basic-site
# or without optional dependencies
npm install basic-site --no-optional
Setup
You can use any view engine you want. By default, this module runs on regve made by AspieSoft. It has a similar syntax to handlebars, but with more features and better stability. It also supports a markdown like syntax.
const server = require('basic-site');
server.pages({
'/url': function(req, res, next){
res.render('index', {opts});
},
'/url2': function(req, res, next){
res.render('url2', {opts});
},
});
const port = 3000;
server(port);
const {join} = require('path');
server.static('/', join(__dirname, 'public'));
server.viewEngine('regve' || 'inputmd', {
template: 'layout',
dir: join(__dirname, 'views'),
type: 'html',
cache: '1D',
});
server.viewEngine(function(app){
app.engine('html', regve({
template: 'layout',
dir: join(__dirname, 'views'),
type: 'html',
cache: '1D',
}));
});
server.pages(function(app){
app.use('/url', (req, res, next) => {
});
app.req('/url2', (req, res, next) => {
});
app.post('/url3', (req, res, next) => {
});
app.get('/url3', (req, res, next) => {
});
});
server.pages({
'/url': function(req, res, next){
},
'/url2': function(req, res, next){
},
});
const port = 3000;
server(port);
Usage
function(req, res, next){
req.startTime
req.static
req.root
req.clean(jsVar)
req.varType(jsVar)
req.validator
req.hostUrl
req.browser
req.uip
req.localhost
req.geo
req.bot
req.url
req.body
req.query
req.data
}
server.randToken(size )
server.clean
server.varType
server.root
server.server
server.express
server.helmet
server.validator
server.geoIP
server.isBot