Node.jsĀ® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
From the perspective of a frontend developer who extensively uses JavaScript,
Node.js apps bring with them a huge advantage: the comfort of programming everything
- the frontend and the backend - in a single language.
recommend you to have a good grasp of the main JavaScript concepts before diving into Node.js:
Lexical Structure, Expressions, Types, Classes, Variables, Functions, this, Arrow Functions
Loops, Scopes, Arrays, Template Literals, Semicolons, Strict Mode
ECMAScript 6, 2016, 2017
With those concepts in mind, you are well on your road to become a proficient JavaScript developer, in both the browser and in Node.js.
The following concepts are also key to understand asynchronous programming, which is one of the fundamental
parts of Node.js:
Asynchronous programming and callbacks, Timers, Promises, Async and Await, Closures, The Event Loop
Many times with Node.js we start servers, like this HTTP server:
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('Hi!')
})
app.listen(3000, () => console.log('Server ready'))
Node Repl |
Replit Browser App |
Node Accept Arguments from the Command Line |
const oranges = ['orange', 'orange']
const apples = ['just one apple']
oranges.forEach(fruit => {
console.count(fruit)
})
apples.forEach(fruit => {
console.count(fruit)
})
In Terminal
npm install webpack webpack-cli
Router Level Middleware
Middle Functions
Notes: Code Snippets Below include express js, routes, path functionality built into node.js,
__dirname "public" as server data is private and not available to the public
const express = require("express")
const app = express()
const path = require("path")
app.use(express.urlencoded({ extended: false }))
app.use(getWeather)
app.use(express.static(path.join(__dirname, "public")))
app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))
function getWeather(req, res, next) {
req.visitorWeather = false
if (req.visitorWeather) {
res.send("Please come back to our app when it is not raining.")
} else {
next()
}
}
app.get("/", (req, res) => {
res.render("home", {
isRaining: req.visitorWeather,
pets: [
{ name: "Meowsalot", species: "cat" },
{ name: "Barksalot", species: "dog" }
]
})
})
app.get("/about", (req, res) => {
res.send("Thanks for learning more about us.")
})
app.post("/result", (req, res) => {
if (req.body.color.trim().toUpperCase() === "BLUE") {
res.send("Congrats, that is correct.")
} else {
res.send("Incorrect, please try again.")
}
})
app.get("/result", (req, res) => {
res.send("Why are you visiting this URL?")
})
app.get("/api/pets", (req, res) => {
res.json([
{ name: "Meowsalot", species: "cat" },
{ name: "Barksalot", species: "dog" }
])
})
app.listen(3000)
Ejs Templating Engine = /views/home.ejs
Document
Welcome to the homepage!!!
What color is the sky on a clear day?
<% if (isRaining) { %>
It is raining.
<% } else { %>
It is not raining.
<% } %>
<% pets.forEach(pet => { %>
<%= pet.name %> (<%= pet.species %>)
<% }) %>
Thursday January 13 2022