servertemplate.js
v3.0.0
Published
Advanced localhost server template framework for Node.js with routing, middleware, logger, static server, and body parser
Maintainers
Readme
servertemplate.js
Advanced localhost server template framework for Node.js.
Installation
Install using npm:
npm install servertemplate.jsQuick Start
Create a server:
const ServerTemplate = require("servertemplate.js");
const app = new ServerTemplate({
port: 3000
});
app.get("/", (req, res) => {
app.text(res, "Hello ServerTemplate v3!");
});
app.start();Run:
npm startServer:
http://localhost:3000Features
- 🚀 HTTP Server
- 🌐 Custom Router
- 🔌 REST Response Helper
- 🛡️ Middleware System
- 📝 Logger System
- 📁 Static File Server
- 📦 Request Body Parser
- ⚙️ Configuration Support
- 🪶 Lightweight Framework
Project Structure
servertemplate.js/
├── index.js
├── package.json
├── README.md
├── LICENSE
└── src/
├── config.js
├── routes.js
├── rest.js
├── host.js
├── logger.js
├── middleware.js
├── static.js
└── body.jsRouting
GET Route
app.get("/hello", (req, res) => {
app.text(res, "Hello World!");
});POST Route
app.post("/user", (req, res) => {
app.json(res, {
user: req.body
});
});Middleware
Add middleware:
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});Flow:
Request
↓
Logger
↓
Middleware
↓
Router
↓
ResponseStatic Server
Serve website files:
app.static("./public");Example:
public/
├── index.html
├── style.css
└── script.jsAccess:
http://localhost:3000/index.htmlBody Parser
Read request body:
app.post("/api/data", (req, res) => {
console.log(req.body);
app.json(res, {
success: true
});
});JSON request:
{
"name": "Fian"
}Result:
{
name: "Fian"
}REST Response
JSON Response
app.json(res, {
status: "ok"
});Text Response
app.text(res, "Hello");Logger
Built-in logger:
[INFO] Server running
[REQUEST] GET /
[ERROR] 404 Not FoundConfiguration
Edit:
src/config.jsExample:
module.exports = {
port: 3000,
host: "localhost",
serverName: "ServerTemplate"
};API
Constructor
new ServerTemplate(options)Options:
| Option | Type | Default | |---|---|---| | port | Number | 3000 | | host | String | localhost | | message | String | Server Template Running |
Methods
| Method | Function |
|---|---|
| app.get() | Create GET route |
| app.post() | Create POST route |
| app.use() | Add middleware |
| app.static() | Enable static files |
| app.json() | Send JSON response |
| app.text() | Send text response |
| app.start() | Start server |
Version
Current version:
3.0.0License
MIT License
Author
Fian
