node.jz Flashcards
In Express, routes takes the following structure: app.METHOD(PATH, HANDLER). METHOD is an http method in lowercase. PATH is a relative path on the server (it can be a string, or even a regular expression). HANDLER is a function that Express calls when the route is matched. Handlers take the form function(req, res) {…}, where req is the request object, and res is the response object.
using the get method
app. get(‘/’, function(req,res){
res. send(‘Hello Express’)
})
using the get method to send a file from index.html
app. get(‘/’, function(req,res){
res. sendFile(__dirname + ‘/views/index.html’);
})
what is the format of a request
HTTP request
What is a static website
A static website is a website that gives information about a certain website but it cannot be altered with or moved
Arrow functions
when we use arrow functions we do not use the function keyword
Arrow function Syntax
The syntax for this is () => { }
What is a Promise
A promise is a JavaScript object that represents the eventual outcome of an asynchronous operation. aPromise has three different outcomes: pending, fullfilled, and rejected.
What does REPl Stand for ?
read–eval–print loop
os methods
example: const server = { type: os.type(), architecture: os.arch(), uptime: os.uptime() } console.log(server)
os. type() — to return the computer’s operating system.
os. arch() — to return the operating system CPU architecture.
os. networkInterfaces — to return information about the network interfaces of the computer, such as IP and MAC address.
os. homedir() — to return the current user’s home directory.
os. hostname() — to return the hostname of the operating system.
os. uptime() — to return the system uptime, in seconds.
In Express, you can put in place this functionality using the middleware express.static(path), where the path parameter is the absolute path of the folder containing the assets.
app.use(path, middlewareFunction)
n Express, you can put in place this functionality using the middleware express.static(path), where the path parameter is the absolute path of the folder containing the assets.
Mount the express.static() middleware to the path /public with app.use(). The absolute path to the assets folder is __dirname + /public.
using middleWare function
app.use((req, res, next) => {
console.log(req.method + “ “ + req.path + “ - “ + req.ip)
next();
})
Middleware can be mounted at a specific route using app.METHOD(path, middlewareFunction). Middleware can also be chained inside route definition.
Look at the following example:
app.get(‘/user’, function(req, res, next) {
req.user = getTheUserSync(); // Hypothetical synchronous operation
next();
}, function(req, res) {
res.send(req.user);
});
This approach is useful to split the server operations into smaller units. That leads to a better app structure, and the possibility to reuse code in different places. This approach can also be used to perform some validation on the data. At each point of the middleware stack you can block the execution of the current chain and pass control to functions specifically designed to handle errors. Or you can pass control to the next matching route, to handle special cases. We will see how in the advanced Express section.
In the route app.get(‘/now’, …) chain a middleware function and the final handler. In the middleware function you should add the current time to the request object in the req.time key. You can use new Date().toString(). In the handler, respond with a JSON object, taking the structure {time: req.time}.
app.get(“/now”, function(req,res,next){
req.time = new Date().toString();
next();
},
(req,res) => {
res.send({
time: req.time
})
})
Build an echo server, mounted at the route GET /:word/echo. Respond with a JSON object, taking the structure {echo: word}. You can find the word to be repeated at req.params.word. You can test your route from your browser’s address bar, visiting some matching routes, e.g. your-app-rootpath/freecodecamp/echo.
app.get(“/:word/echo”, (req,res) => {
const{word} = req.params;
res.json({ echo: word }) })