Node JS Notes
Node.js is a JavaScript runtime environment that executes JavaScript code outside of a browser. It is built on Chrome’s V8 JavaScript engine and uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js is often used to develop web servers and networking applications, but it can also be used to develop desktop and mobile applications.
Node.js Install (Download & Install)
To install Node.js on your computer, you can visit the Node.js website and download the latest version for your operating system. Once you have downloaded the installer, run it and follow the on-screen instructions.
Import and Export in Express
Express is a popular Node.js framework that makes it easy to build web applications. Express uses modules extensively, and you can import and export modules from your Express applications just like you would in any other Node.js application.
Example:
JavaScript
// export a function from a module
module.exports = function hello() {
console.log('Hello, world!');
};
// import the function into another module
const hello = require('./hello');
// call the function
hello();
Use code with caution. Learn morecontent_copy
Run The Code
To run Node.js code, you can use the node
command. For example, to run the code above, you would type the following command in a terminal:
node hello.js
This would print the message “Hello, world!” to the console.
What is NPM
NPM is a package manager for Node.js. It allows you to install, uninstall, and manage Node.js packages, which are reusable modules of code.
Install Package Using NPM
To install a Node.js package using NPM, you can use the npm install
command. For example, to install the Express framework, you would type the following command in a terminal:
npm install express
This would install the Express package into your project’s node_modules
directory.
What is Express
Express is a popular Node.js framework that makes it easy to build web applications. It provides a variety of features, such as routing, middleware, and templating.
Routing
Routing is the process of matching HTTP requests to specific functions in your application. Express makes it easy to define routes for your application.
Example:
JavaScript
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Use code with caution. Learn morecontent_copy
This code defines a simple route that will match all HTTP GET requests to the root path (/
). When a request is received, the code will send the message “Hello, world!” to the client.
Difference Between GET & POST
GET requests are used to retrieve data from a server. POST requests are used to send data to a server.
Example:
JavaScript
app.get('/users', (req, res) => {
// Get a list of users from the database
const users = [
{ name: 'Alice' },
{ name: 'Bob' },
{ name: 'Carol' },
];
// Send the list of users to the client
res.send(users);
});
app.post('/users', (req, res) => {
// Create a new user in the database
const user = {
name: req.body.name,
};
// Save the user to the database
// Send a response to the client
res.send('User created successfully');
});
This code defines two routes: one for getting a list of users and one for creating a new user. The first route uses the GET method, while the second route uses the POST method.
First Express Code
Here is a simple Express application that you can use to get started:
JavaScript
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, world!');
});
app