yanef
v1.2.2
Published
Yet Another Express Framework
Maintainers
Readme
yanef
Yet Another Node Express Framework – a lightweight auto-loader for Express routes.
Installation
npm install yanefQuick Start
Project Structure
my-app/
├─ index.js
├─ routes/
│ └─ home.js
├─ views/index.js
import yanef from "yanef"
let Server = yanef("./routes", 8080)
Server.listen()Defining Routes
Route files must export a default object with a path and an array of routes. Each route contains a method and handler.
routes/home.js
export default {
path: "/home",
routes: [
{
method: "get",
handler: (req, res) => res.send("Hello from /home")
},
{
method: "post",
handler: (req, res) => res.json({ posted: req.body })
}
]
}as well as arrow functions, handler can be expanded to support middleware.
routes/home.js
export default {
path: "/home",
routes: [
{
method: "get",
handler: [
(req, res) => {
console.log("GET /home");
res.end("hello world!")
}
]
}
]
}Manual Routes
You can still register manual routes in your entry file.
index.js
import yanef from "yanef"
let Server = yanef("./routes", 8080)
Server.app.get("/ping", (req, res) => res.send("pong"))
Server.listen()Static Files
If you provide a directory path, it will be used both for loading route files and serving static files.
index.js
import yanef from "yanef"
let Server = yanef("./views", 8080)
Server.listen()This will serve static files from ./views and also load any .js files inside as routes.
License
MIT
