Basic Site



A module that runs express with an easy setup and additional basic security for a small and simple 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 users geo IP with the geoip-lite 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 any extra / at the 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 (this size can be modified).
- 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.
Whats New?
- Added a new view engine turbx
- Added a server.path function to safely resolve and join paths without backtracking
- Added an option to change the memory limit of user requests
- Added PWA Support
- Added option to automatically minify js and css files from public/static directory
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.
This module now supports turbx beta made by AspieSoft. It is a newer view engine than regve, and has an xhtml like syntax. It also supports a component system you can pass variables into. To use it, simply run server.viewEngine('turbx');
before starting the server.
const server = require('basic-site');
server.viewEngine('turbx');
server.pwa({name: 'App Name', short_name: 'App', icon: 'favicon.ico', icon_background: '#ffffff'});
server.minify(['js', 'css']);
server.minify();
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);
server.static('/', server.path(__dirname, 'public'));
server.viewEngine('regve' || 'inputmd', {
template: 'layout',
dir: server.path(__dirname, 'views'),
type: 'html',
cache: '1D',
});
server.limit('1mb');
server.limit(10);
server.viewEngine(function(app){
app.engine('html', regve({
template: 'layout',
dir: server.path(__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.limit
req.clean(jsVar)
req.varType(jsVar)
req.joinPath('path', 'to', 'file', 'from', 'app', 'root')
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.path('path', 'to', 'file', 'from', 'app', 'root')
server.clean(userInput)
server.varType(myVar)
server.root
server.server
server.express
server.helmet
server.validator
server.geoIP
server.isBot