Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit

Create A New Node.js Project

How To Create New Node.js Project npm init

Hello in this article, I will share the information about the How To Create New Node.js Project. We had already learned what is express module in node.js in previous article.

Firstly, We download and install node.js from nodejs.org. Then we download and install Visual Code Studio from https://code.visualstudio.com/. After that we will open Visual Code and open a new folder named 'sirnode-example-project' in Viusal Code.

Creating new project on viusal studio
Opening Project in Visual Code
What is node.js
Creating new project named SIRNODE-EXAMPLE-PROJECT.

After creating new project, we can open terminal. Today, many people who write a node.js project and work with the express framework have to write the basic code from scratch when they create a new project. Like: add app.js, open route folder, add route.js, like public open css, add javascript codes. With Express generator, it offers you this ready structure directly with a single code. Its setup is very simple. I am using the npm package manager, if you do, you can continue.

We should open terminal then write npm install express-generator -g. Npm install command provides installing libraries. We installed express-generator library that generating automatically public, routes, bin, views folders and app.js, package.json files.

After that we should write express . in terminal. Then we can see generated folders and files in project.

What is node.js
Generated folders and files automatically in sirnode-project.

What is routes folder? What does it do?

As is known, there are four basic Http methods. These; GET, POST, PUT, DELETE. In Express.js, there is an "ALL" method in addition to these basic http methods. This method has been created in a way that it can accommodate the functions of other methods alone to meet all incoming requests. Routing routes are created using these methods in Express.js. Let's quickly illustrate this here, as we discussed in our previous articles.

const express = require("express"); 
const app = express();
app.get("/getUsers", (request, response) => {
   response.send(`${request.url} : GET`);
}).post("/createUser", (request, response) => {
   response.send(`${request.url} : POST`);
}).put("/updateUser", (request, response) => {
   response.send(`${request.url} : PUT`);
}).delete("/deleteUser", (request, response) => {
   response.send(`${request.url} : DELETE`);
});
app.listen(8000, () => {
     console.log("Sir Node is Here..");
});

As you can see, route structures are created as above.

What is the node_modules folder? What does it do?

With the express module we have installed, you will see that there is a folder named node_modules in the project folder and many subfolders and files. These folders are the dependencies of the express module, which we call dependencies, that is, the modules it needs to run.

Express is a node.js project, just like we are doing now, and it contains the package.json file. While installing Express, it downloads the modules from the dependencies section in the package.json file to our project.

This is the case with all node.js projects. In addition, you will find that the node_modules folder is often not found in Github repositories because it takes up a lot of file content and space. This is not a problem. After downloading any node.js project from Github, when you open a console in the same folder and run the npm install command, this folder will be created automatically and download the necessary files.

As a result, in this article we learned how to How To Create New Node.js Project. If you want to learn more, keep in touch with sirnode.