Creates a NRAF application. The NRAF()
function is a top-level core function exported by the @nraf/core
package.
const NRAF = require("@nraf/core");
const app = NRAF();
The app
object conventionally denotes the Nraf application. Create it by calling the top-level NRAF()
function exported by the nraf/core module:
var NRAF = require("@nraf/core");
var app = NRAF();
app.get("/", function (req, res) {
res.send("hello world");
});
app.listen(3000);
Assigns setting name
to value
.
The following table lists application settings:
Property | Type | Description | Default |
---|---|---|---|
“views” | String | Path of the folder containing the views, i.e. the .nraf extension files for the template-engine to parse. | null |
“public” | String | Path of the folder containing public assets such as robots.txt, favicon, etc. | null |
NOTE: You must set the “views” & “public” directory path explicitly in your app. For more details, read this.
app.set("views", path.join(__dirname, "views"));
app.set("public", path.join(__dirname, "public"));
Routes HTTP GET requests to the specified path with the specified callback functions.
Argument | Description |
---|---|
path | A path for which the middleware function is invoked |
callback | A callback function to be executed which takes the response and request objects as parameters. |
app.get("/", (req, res) => {
res.json(todos);
});
Routes HTTP POST requests to the specified path with the specified callback functions.
Argument | Description |
---|---|
path | A path for which the middleware function is invoked |
callback | A callback function to be executed which takes the response and request objects as parameters. |
app.post("/add-todo", (req, res) => {
TODOS.push(req.body.todoContent);
res.redirect("/");
});
Mounts the specified middleware function.
app.use((req, res, next) => {
console.log("Time: %d", Date.now());
next();
});
This method is identical to Node’s http.Server.listen().
const PORT = process.env.PORT || 3000;
...
...
app.listen(PORT, () => {
console.log("Server is running on PORT: ", PORT);
});
Renders a view and sends the compiled HTML string to the client.
The view
argument is a string that is the file name of the view file to render.
locals
is an object whose properties define local variables for the view.
NOTE: You should set the “views” directory path explicitly in your app. For more details, read this.
Read more about the nraf template engine.
res.render("home", {
todos: TODOS,
isTodoEmpty: TODOS.length === 0,
});
In the above example, home
is the view passed which is same as the name of the file in the views directory.
Redirects to the URL derived from the specified path
.
It by default sets the HTTP status code to 302.
res.redirect("/");
Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().
res.json({
idx,
content,
});
Sets the response HTTP status code to statusCode
. If an unknown status code is specified, the response body will just be the code number.
res.setStatus(302);
This method is identical to Node’s response.write().
res.send("<p>some html</p>");
Ends the response process. This method comes from the response.end() method of http.ServerResponse.
res.end();
The URL path on which a instance was mounted.
app.get("/", (req, res) => {
console.log(req.query);
res.render("home", {
todos: TODOS,
isTodoEmpty: TODOS.length === 0,
});
});
// Console output: "/"
Contains key-value pairs of data submitted in the request body.
app.post("/add-todo", (req, res) => {
TODOS.push(req.body.todoContent);
res.redirect("/");
});
This property is a JS object containing properties mapped to the named route “parameters”. For example, if you have the route /books/:bookId
, then the bookID
property is available as req.params.bookID
. This object defaults to {}
.
// Route path: /users/:userId/books/:bookId
// Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
This property is a JS object after the query string is parsed. This object defaults to {}
.
// Route path: */user?name=tom&age=55*
req.query: {name:"tom", age: "55"}