Failed to open my server by Node.js

Asked

Viewed 92 times

-1

Today I had my first contact with Node.js and the sequence of the video lesson was like this:

npm init -y // no terminal do visual studio (Criou o package.json que nem na vídeo aula)
npm install express // no terminal (Criou as pastas do express e o package-lock.json e apareceu a dependência no package.json)

Then the teacher said he was going to start the server so a file was made server.js with the command

require('express')().listen(5500)

Ai in the terminal was made the command that I could not accomplish

node src/server.js 

It should appear a permission bid from node.js and allow me to open the server but it won’t go at all! No one I know node.js, some of you know what I did?

1 answer

0

I don’t quite understand the question of permission you mentioned.

The only thing done in his code was to instantiate a server that would run on port 5500 - http://localhost:5500/. However since you do not have any route, when accessing this address should receive Cannot GET /.

Getting back to your problem, turning the node src/server.js you were in the root folder ? if you are in the src folder, you should just run node server.js. This command will not return anything on your terminal, the terminal will get frozen because it will be processing the server on the mentioned port.

For you to have this happening in a more "visual" way, change your file server.js for that reason:

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

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(5500, () => {
  console.log(`O servidor está rodando na porta 5500`)
})

const express = require('express') and const app = express() we are declaring that the app variable will be an instance of express.

app.get('/' ... We are informing that when hitting the root of our server (a /), will be returned only a text written "Hello World". As you advance in the study you will better understand what they are req, res, res.send, etc..

and finally in app.listen(5500, ... we are informing that we will run the server on port 5500, with a callback that will print in the terminal the information O servidor está rodando na porta 5500.

Remember to turn around node src/server.js if it is in the root folder or node server.js if it’s in the folder src.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.