Getting Started NodeJs

Getting Started with Node.js: A Beginner's Guide

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows developers to use JavaScript to write server-side code.

Key Features:

  • Asynchronous and Event-Driven: All APIs in Node.js are asynchronous, meaning non-blocking.

  • Single-Threaded but Highly Scalable: Uses a single-threaded model with event looping, making it highly scalable.

  • Rich Ecosystem: With npm (Node Package Manager), Node.js has a vast library of packages and modules.

Getting Started:

  1. Installation:

    • Download and install Node.js from the official website.

    • Verify installation by running node -v and npm -v in your terminal.

  2. Creating a Simple Server:

    1 2 3 4 5 6 7 8 9 10 11 const http = require('http'); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello, World!\n'); }); server.listen(3000, '127.0.0.1', () => { console.log('Server running at http://127.0.0.1:3000/'); });

  3. Running Your Server:

    • Save the file as app.js

    • In your terminal, run

      1 node app.js