node js hello word

  1. Create a new directory for your Node.js project:
  2. Open your terminal or command prompt.
  3. Navigate to the desired location where you want to create your project.
  4. Run the command mkdir node-project to create a new directory called "node-project".

  5. Initialize a new Node.js project:

  6. Run the command cd node-project to enter the project directory.
  7. Run the command npm init to initialize a new Node.js project.
  8. Follow the prompts to provide information about your project, such as its name, version, description, entry point, etc.

  9. Install Express.js:

  10. Run the command npm install express to install the Express.js framework.
  11. Express.js is a popular framework for building web applications with Node.js.

  12. Create the main JavaScript file:

  13. Run the command touch index.js to create a new JavaScript file called "index.js".
  14. This file will serve as the entry point for your Node.js application.

  15. Write the code for a simple "Hello, World!" server:

  16. Open the "index.js" file in a code editor.
  17. Add the following code to create a basic Express.js server:

    ```javascript const express = require('express'); const app = express();

    app.get('/', (req, res) => { res.send('Hello, World!'); });

    app.listen(3000, () => { console.log('Server running on port 3000'); }); ```

  18. This code imports the Express.js module, creates a new Express application, defines a route for the root URL ("/") that sends the response "Hello, World!", and starts the server on port 3000.

  19. Start the Node.js server:

  20. Run the command node index.js to start the Node.js server.
  21. You should see the message "Server running on port 3000" in the terminal, indicating that the server is running successfully.

  22. Test the server:

  23. Open a web browser and navigate to http://localhost:3000.
  24. You should see the message "Hello, World!" displayed in the browser, confirming that the server is responding correctly.

That's it! You have successfully created a simple "Hello, World!" server using Node.js and Express.js.