How do I get started with Node.js?

 

Getting started with Node.js is relatively straightforward. Node.js is a JavaScript runtime that allows you to run JavaScript code on the server-side. Here are the steps to get started with Node.js along with a simple "Hello, World!" example:

  1. Installation:

    • Visit the official Node.js website: https://nodejs.org/
    • Download the Node.js installer for your operating system (Windows, macOS, or Linux).
    • Run the installer and follow the installation instructions.
  2. Verify Installation:

    • After installation, open a terminal or command prompt.
    • Run the following commands to verify that Node.js and npm (Node Package Manager) are installed:
    bash
  • node -v # Check Node.js version npm -v # Check npm version

    You should see the version numbers displayed in the terminal.

  • Create a Simple Node.js Application:

    • Create a new directory for your Node.js application and navigate to it in the terminal.
    bash
  • mkdir my-node-app cd my-node-app
    • Create a new JavaScript file, for example, app.js, using a text editor or code editor of your choice.
    javascript
  • // app.js const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello, World!\n'); }); const port = 3000; server.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); });
  • Run the Node.js Application:

    • In your terminal, run the following command to start your Node.js application:
    bash
    1. node app.js

      You should see the message "Server is running at http://localhost:3000" in the terminal, indicating that your Node.js server is running.

    2. Access Your Application:

      • Open a web browser and visit http://localhost:3000.
      • You should see "Hello, World!" displayed in your browser, indicating that your Node.js server is serving content.

    Congratulations! You've successfully created and run a simple Node.js application. This "Hello, World!" example demonstrates the basic structure of a Node.js application, including setting up an HTTP server.

    From here, you can explore Node.js further by learning about its modules, packages, and how to build more complex applications. The Node.js website and documentation are excellent resources for learning and getting started with Node.js development: https://nodejs.org/en/docs/

    Comments