Starting with Node.js – It’s as Easy as Pie!
Step 1: Setup Your Lair (AKA Your Project Directory)
Just like every superhero needs a lair, every Node.js project needs a directory. Create a folder on your computer and name it something cool like “NodeNest” or “JavaScriptJungle”.
Step 2: Initiate the Secret Handshake (Run npm init
)
Open your trusty sidekick, the terminal, and navigate to your project folder. Run npm init
and answer the mystical questions. Voila, you’ve just created your package.json
, the manifesto of your project!
Step 3: Writing Your First Spell (A Simple Script)
Create a file named app.js
. Now, let’s write a spell that conjures a web server:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
const port = 3000;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
Step 4: Bring it to Life! (Run Your App)
In the terminal, chant node app.js
, and like magic, your server is alive!