I know, I know... Another Node Express tutorial 😑.
But if you still haven't got around to learning Node and building a server, maybe this super-quick tutorial is the one you're waiting for! 😆
Step 1: Install node
Ok, this must take more than 5 minutes, but if you have Node already, skip this and let's go!
To test if it works, create a file server.js in your project root
// server.jsconsole.log("Hello World!");
And test it out
$ node server.js
Hello world!
Nice! We're ready to do some backend-fu!
Step 2: Setup npm
We use NPM to manage our node packages.
Initialize npm and let it take defaults.
$ npm init -y
Step 3: Install and import express middleware
A middleware is a piece of code that has access to the request and response object. For now, think about express making things easier for us by being a "middle-man" 🕵️ between our code and Node's HTTP stuff.
constexpress=require('express');constserver=express();constport=4000;server.get("/",(req,res)=>{res.sendFile(__dirname+'/index.html');});server.get("/json",(req,res)=>{res.json({message:"Hello world"});});server.listen(port,()=>{console.log(`Server listening at ${port}`);});
"Okay, that was nice. But what can I do with this? "
Add a few more routes and HTML pages
and you got yourself an HTML + JSON server!
Build a server and API for your next web application, using Node, Express and MongoDB
Node workshop
Create a server + REST API for your next web application!
In this workshop, we’ll discuss concepts and put them to practice with activities, all about web servers
The code and concepts here would be a great foundation for your next web project
Topics include, but not limited to:
Using Node and Express to build a web server and REST API
Understanding routing, request and response
Implementing CRUD with HTTP methods
Building a server-rendered website using templates
Connecting to a Cloud NoSQL database: MongoDB Atlas DB
User authentication with sessions, cookies and tokens
Using external APIs, such as Github Jobs, Giphy, Spotify