bambangindrawahyudi / node-express-course

An introduction to Node.js and Express.js servers
https://lab.github.com/everydeveloper/introduction-to-node-with-express
0 stars 0 forks source link

Start express server #3

Closed github-learning-lab[bot] closed 4 years ago

github-learning-lab[bot] commented 4 years ago

Alright, now let's start making a server! Open up your server.js file and add these two lines:

const express = require('express');
const app = express();

The first line gives you access to the express library by searching your node_modules for "express". The next creates an instance of the express constructor, which we will name "app". We can now access methods used for making a server by including their name after app. Add this to the bottom of server.js:

app.listen(8000,function(){
console.log("server is running")
})

The app.listen method will start up the server locally on the port you give as its first argument (in this case the base url is: http://localhost:8000)

But first we need to run the server.js file by entering this in the terminal: node server.js

If everything was successful, you should see the console.log message we supplied in the callback: "server is running". This happens because the file is being run on our terminal. To end this process, push CTRL+C. Whenever you make changes to your server, you need exit and restart it.

Once your server is working, push your changes up to GitHub to complete this step.

git add server.js
git commit -m"set up express server"
git push origin master
github-learning-lab[bot] commented 4 years ago

You just used Node.js to run a script outside of the browser!

💡 You can reset the server automatically when you make changes using a tool called nodemon

With this last push, your repository should look like this.

Click here for the next step