Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start express server #3

Closed
github-learning-lab bot opened this issue Nov 26, 2020 · 1 comment
Closed

Start express server #3

github-learning-lab bot opened this issue Nov 26, 2020 · 1 comment

Comments

@github-learning-lab
Copy link

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants