Everything you need to know to build your first server in Express
When I was first starting to learn Node.js and express it was very intimidating as all the tutorials I saw had me building medium to large scale applications. In this tutorial, we’re gonna keep it simple and build a basic server that sends “Hello World!” when the server is started.
Before we start our journey as a Node.js developer. We must first do some initial project setup
All we need to do for this is initialize npm in our project and install express into our app. The code for this is shown below:
Note: the _-y_
after _npm init_
allows us to skip filling out the package.json and get straight into coding
npm init -y
npm install express
After this in your directory create a new file called server.js_. _This is where all the code for our server will go.
Now that we have to express added installed through npm we have to actually require it in our project.
In your server.js file require the express module and store it in a variable called express.
We will have another variable that will hold the port our server runs on when it is running locally. This variable will unoriginally be called port
After this, we will create another variable called app and make it reference express(). This app variable is essentially the top layer of our server and will be how we add routes and eventually start the server
#expressjs #nodejs #servers #software-development