Simple NodeJS (Express) HTTP server ready to use for landing pages (static HTML) or just a public cloud storage.
This is a simplified version of one of my NodeJS based servers without any extra dependency (just express
and custom modules).
About
- Request logger (log on response)
- Configuration file (similar to .env)
- Multi language (+ RTL) support (actually it's useless since there's no view engine in this project)
- Public directory (static files)
- Custom error pages (404 and 500)
As mentioned before, there is only one dependency and it's express
. So you may change things and add modules to build your desired app.
For example a complete project may contain:
- View Engine (EJS, ...)
- Layout for views
- Cookie/Body parser
- Database (MongoDB, Mongoose, ...)
- Auth (Passport, JWT, ...)
- Security (Helmet, CORS, CSRF, Rate limiting, custom header config, ...)
Directories
extensions.js
Functions that are bound on string prototype (Persian digits to English digits and reverse), check isRTL for user language (useless in this project), process kill/close/end/error function, ...
overlap.js
Check if two number ranges have overlap (not used)
pagination.js
Pagination function that splits an array (not used)
multiLang.js
This module exports an object.
The JSON object has languages keys (for exampleen
lang code) and each lang code must have all key:value pairs of your application texts. For example"helloWorld": "Hello World!"
.
I've added two keys named__fontGroup
and__fullname
which can be used for CSS fonts and logic in CSS or views. For example Persian and Arabic can use the same font group.
You can generate this JSON object or just use this static structure.
Used for serving static files like CSS, JS, images and...
Don't put private files here.
HTML files (index, errors) are placed here but since there's no view engine you can just move these files to
public
directory and change the paths inapp.js
.
Files
Similar to .env
.
const config = {
appName: "", // Application name
version: "", // Version code
versionName: "", // Version name
title: "", // Application title (can be used in title tag)
defaultLang: "", // Default language code to select from multiLang.js
rtlLangs: [], // List of language codes that are right-to-left (e.g. 'fa')
capitalizeLangs: [], // List of language codes that can be capitalized (e.g. 'en')
reverseProxy: Boolean(), // true if server is using reverse proxy (sets express trust proxy)
port: Number() // HTTP server port (e.g. 3000)
};
Starts HTTP server from app.js
module
Main application.
Import experss
, cfg.js and multiLang.js.
Runs extensions.js
from functions.
Express settings (remove x-powered-by from header, set trust proxy, set public directory, ...)
Main middleware:
- Logs request info on response (IP, path, method, res code, res time, user agent)
- Sets UIText on res.locals from multiLang.js, based on request query (can be res.user object)
- Sets current path and config object to res.locals
Bind /
route (get
method) to views/index.html
And finally et error codes