a-quick-intro-to-nodejs
a-quick-intro-to-nodejs
Quick introduction to NodeJS
This article introduces NodeJS, one of the most popular and powerful Javascript server frameworks. Hopefully after
reading/following this, you will decide to explore further into its capabilities. Happy coding!
1. What is [Link]?
[Link] is a JavaScript runtime built on Chrome's V8 engine that allows you to run JavaScript on the server side. It is
great for building scalable, event-driven applications, such as web servers, APIs, and real-time applications.
2. Install [Link]
Visit the [Link] website and download the LTS (Long Term Support) version.
After installation, verify by running:
node -v
npm -v
npm is Node's package manager, which helps install libraries and dependencies.
3. Create a Simple Web Server
1. Create a new folder for your project:
mkdir node-tutorial
cd node-tutorial
2. Initialize a new [Link] project:
npm init -y
This will create a [Link] file that stores project settings and dependencies.
3. Create a simple web server:
Create a file called [Link] :
// [Link]
const http = require('http');
const server = [Link]((req, res) => {
[Link] = 200;
[Link]('Content-Type', 'text/plain');
[Link]('Hello World\n');
});
const PORT = 3000;
[Link](PORT, () => {
[Link](`Server running at [Link]
});
1/3
a-quick-intro-to-nodejs
4. Run the server:
node [Link]
Visit [Link] in your browser to see the "Hello World" message.
4. Using NPM Packages
Let’s use a package called express , a popular web framework for [Link].
1. Install express:
npm install express
2. Update [Link] :
const express = require('express');
const app = express();
[Link]('/', (req, res) => {
[Link]('Hello from Express!');
});
const PORT = 3000;
[Link](PORT, () => {
[Link](`Server running at [Link]
});
3. Run the server again:
node [Link]
You should now see "Hello from Express!" at [Link]
5. File Structure Best Practices
Organise your project like this for larger applications:
node-tutorial/
│
├── [Link]
├── [Link]
└── routes/
└── [Link]
└── controllers/
└── [Link]
6. Nodemon for Auto-Restarting
To automatically restart the server when you change files, use nodemon :
1. Install globally:
2/3
a-quick-intro-to-nodejs
npm install -g nodemon
2. Run the server with nodemon:
nodemon [Link]
7. Conclusion
You’ve set up a simple [Link] project with a web server and Express. From here, you can explore more advanced
features like working with databases (MongoDB, MySQL), middleware, and deploying applications.
Reference
[Link]
3/3