Creating your first web app with Node.js

deepika_banoth profile image Deepika Banoth ・2 min read

Hello everyone! I hope you are all doing great. πŸ™‚

Today we will create a very simple web application using Node.

Let's get started! πŸ™‚

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." – Martin Golding

This is one of my favorite quotes. And actually it is not a bad idea to write a clean code. If you are looking back at your code after some months or years, at least you should be able to understand what you wrote.

So, lets first create a very simple file structure which will help in managing it easily. We will be putting most of the code of our Node application into the server.js file.

Alt Text

However, for larger applications, server.js should be broken down further to separate duties.

Installing Modules

The package.json holds configuration of our application. Node’s package manager (npm) will use this to install and dependencies or modules that we are going to use.

Alt Text

Now open your terminal, go to your project and run npm install, npm will look at this file package.json and install all dependencies.

Node Configuration

Our main file server.js will configure the app for Express, and listening on a port.

Alt Text

Start Your Application

Now that we have package.json and server.js started up, we can start up our server and see whats going on. Just go to your project folder in your terminal and type command node server.js. Now you have a server listening on a port 3000. Going to http://localhost:3000, you should be seeing something like this:

Alt Text

You can also add a html which has code of our main view

Alt Text

and use the path in the server.js.

Alt Text

Note: The path you mention here must be absolute.

Alt Text

I hope this post helps πŸ™‚


markdown guide