Node.js Backend Basics · Lesson 1 of 6
JavaScript on the server
Understand what Node is, what a backend does, and run your first server.
Prerequisite, stated plainly: javascript-basics. Node is JavaScript, so weak JavaScript means learning two things at once.
What Node is: the JavaScript engine from Chrome, taken out of the browser and given the ability to read files, listen on network ports and talk to databases. That is the whole idea. The language you already know now runs on a server, which is why a JavaScript developer can work on both halves of an application — the reason the MERN stack exists and the reason Pakistani software houses hire for it.
What a BACKEND actually does, since this is the half you have not seen: it holds the data everyone shares, enforces the rules nobody should be able to bypass, and keeps the secrets. Your React app runs on the user's phone where they can read every line of it — so the backend is where 'only an admin may delete this', 'this password is correct', and the database credentials live. php-basics made the same argument for PHP; this is that layer in JavaScript.
SETUP: install Node LTS from nodejs.org (you may already have it from react-basics — the same install). Check with node --version. Then the first server, in a file called server.js:
const http = require('http');
http.createServer((req, res) => {
res.end('Assalam o Alaikum from the server');
}).listen(3000);
Run node server.js, open localhost:3000, and your own machine is now serving a web response. Notice that nothing in the browser produced this — the code ran on the server and only its output travelled, exactly as it did in PHP.
NPM is Node's package manager and it arrives with Node. npm init -y creates package.json, the file that records what your project depends on; npm install express adds a package and records it. The node_modules folder it creates is enormous and is never committed to git — .gitignore excludes it, and npm install rebuilds it from package.json on any machine. That is the whole reason package.json matters.
The raw http module above is educational and nobody writes production code with it. Lesson 2 replaces it with Express, which is what the jobs actually name.
Install Node, create a project folder, run npm init -y, write and run the raw http server above, and visit it in a browser. Then install express and confirm it appears in package.json's dependencies. Add a .gitignore containing node_modules before you commit anything.
Check what you learned
Create your free BvLogic ID to take the quiz and record your score.
Create your BvLogic ID