This is the boilerplate code for the Basic Node and Express Challenges. Instructions for working on these challenges start at https://www.freecodecamp.org/learn/apis-and-microservices/basic-node-and-express/
Express has two main objects to work with: req
and res
.
The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.
The properties of the req
object can be found here
The res object represents the HTTP response that an Express app sends when it gets an HTTP request.
The properties of the res
object can be found here
Express routes use the following template:
app.METHOD(PATH, HANDLER)
where:
METHOD
is an HTTP method in lowercasePATH
is a relative path on the server (Can be string or regex).HANDLER
is the function Express calls when the route matches.- HANDLER takes the form of
function(req, res) {...}
- example:
app.get("/", (res, req) => {...})
for arrow functions - example:
app.get("/", function(res, req))
for traditional functions
- example:
- HANDLER takes the form of
app.send()
allows you to send a stringapp.sendFile()
allows you to send a file, the headers are automatically appliedapp.static(path)
wherepath
is the absolute path of the folder containing the asset allows you to call static assets such as stylesheets, scripts and images. This is a middleware function, which are functions that intercept route handlers and add some kind of information. These functions must be mounted usingapp.use(path, middlewareFunction)
, the first path is optional. If it is not set, the middleware will be applied to all requests.- use:
app.use(express.static(__dirname + "/path"))
andapp.use("/path", express.static(__dirname + "/path"))
to allow anything in the folder "path" be used when "/path" is mentioned
- use:
REST stands for REpresentational State Transfer. A REST API allows for data exchange without clients needing to know details about the server itself. All the client will need is the url where the resource is and the action they want to perform on it (ie. GET from localhost:3000/test). (more verbs)
GET
- fetch info without modifying anythingPOST
- submits an entity to the specific resourcePUT
- replaces all current representations of the resource with the request payloadDELETE
- deletes specific resourceHEAD
- asks for a response identical to theGET
but without the response bodyCONNECT
establishes a tunnel to the server identified by the target resourceOPTIONS
describes the communication options for the target resourceTRACE
performs a message loop-back test along the path to the target resourcePATCH
applies partial modifications to a resource
Middleware functions take 3 arguments, the request object, the response object and the next function in the application's request-response cycle. Meaning the functions execute code that can have side effects on the app, like adding information to the request or response object. They can also end the cycle by sending a response when some condition is met. If they don't send the response when done, they start the execution of the next function in the stack, which is triggered by the 3rd argument next()
.
function(req, res, next) {
console.log("I'm a middleware...");
next();
}
the above middleware function will print the string I'm a middleware...
and then executes the next function in the stack, when a request matches the route.
You can specify when the middleware has to be used: app.use(middlewareFunction())
for execution at the root level, app.METHOD(middlewareFunction())
if the function should only be executed for a specific method (GET, POST, DELETE etc.).