NodeJS Flashcards
What can Node do which old JS can’t?
It can interface with a computer’s hardware, which is why Electron uses node as an example and why it can act as a webserver.
How can I enable ES 6 imports in Node
Set the type
property in your package.json
to module
. Typescript can do it by default, as it’s one of its features.
What is commonJS
It’s node’s way of importing other files into a different file by using require('./whatever)
- extension not required.
In the other file use module.exports = ‘anything’
Anything as a variable:
module.exports = 5;
const number = require(‘./module’);
OR
module.exports = {
someFunction(){
}
}
const {someFunction} = require(‘./someModule’);
someFunction();
const someModule = require(‘./someModule’);
someModule.someFunction()
What does a bare-bones express app require
//import express
const express = require(‘express’);
OR import with type=’module’
import express from ‘express’;
const app = express();
app.get(‘/’,(req,res)=>{res.send(‘some content’)})
app.listen(‘3000’);
SETUP body-parser in order to get the POST body from post requests. - makes req.body available
const bodyParser = require(‘body-parser’);
//middleware
app.use(bodyParser.urlencoded({extended:true}));
REMEMBER
Form data is parsed as strings when sent via HTML form
Different res methods
These set the response Content-Type
header to application/json or text/html.
Second arg is the status code for example 404 - but deprecated
Using res.status(300).send(‘sdffds’) now preferred
res.json() - json
res.send() - html
res.sendFile() - set file path
res.render() - dynamic rendering with ejs for example
What is __dirname and it’s purpose
It’s to work with relative file paths.
__dirname refers to the absolute path of the folder of the script that’s executing it.
When is __dirname not available?
In ES6 module files, ie, when type in package.json is set to module
What does req contain?
req.path - any path variables defined with app.get(‘/blablabla/:someVar/’). Ideal for slugs.
req.body - if body parser is set up
req.query - when query parameters added - users?name=albert
How to catch any path not defined and what is important?
With wildcard
It should be at the end of the router script, otherwise sub-sequent paths will not be reached.
app.get(‘,()=>{});
app.post(‘’,()=>{})
app.put(‘’,()=>{})
app.patch(‘,()=>{})
app.delete(‘*’,()=>{})
Explain HTTP methods/verbs
POST - to update
PUT - replace data
PATCH - update data
DELETE - removed data
GET - retrieve data
What is a popular template engine?
EJS
What is a template engine?
It makes it easy, and less dirty, to dynamically generate HTML, with variable values and conditionals
How do you configure a public path for static files
This is for serving static items like javascript, images, CSS etc
app.use(express.static(‘public’))
Now all files in the public directory are accessible:
public/styles.css
<link></link>
What is middleware in Express
Use app.use() to set middleware
It is a function that is run between the request being received and the response being sent back. It is fired even before the route function.
It allows you to for example do authentication and authorization of routes.
It also allows body-parser for example to intercept the request before it reaches the routes.
How to implement middleware function
app.use((req,res,next)=>{
// call next to go to next middleware or eventually the route if there is no subsequent middleware
next();
})
If next() is not invoked or a response is not returned, it will hang until the request times out.