A web framework for Node.js
Create a package.json
file and add express as a dependency at the CLI
npm init
npm install express --save
var express = require("express");
var app = express();
The view engine
setting determines the file extension
app.set("view engine", "ejs");
res.render(view [, locals] [, callback])
Renders a view and sends the rendered HTML string to the client (Ref: 232)
key: name reference to view file properties: local variables for the view.
res.render("home");
res.render("campgrounds", {campgrounds:campgrounds});
res.send()
only output plain textapp.get(path, callback [, callback ...])
Routes HTTP GET requests to the specified path with the specified callback functions.
res.send([body])
Sends the HTTP response.
app.get("/", function(req, res){
res.send("Hi there!");
});
app.get("/bye", function(req, res){
res.send("Goodbye!");
});
See <a href="../../RESTful Routes/tbl_restful_routes.html" target = "_blank">RESTful routing</a>
app.get("/r/:subRedditName", function(req, res){
var subReddit = req.params.subRedditName;
res.send("Welcome to the " + subReddit.toUpperCase() + " Sub Reddit!!!");
});
Access a route that isn't defined above
app.get("*", function(req, res){
res.send("You are a star!!");
});
app.listen(3000, function(){
console.log("Server has started!!")
});
Directory | Contains |
---|---|
model | Database Schema files |
public | CSS files |
views | EJS Templates |
views/partials | header, footer, navbar EJS templates |