Why this GET request is not working?

129 Views Asked by At
1

There are 1 best solutions below

0
On

That should work. Unless..

Make sure you npm install express

Since your file is named app.js make sure in your package.json file it reads something like this.

Make sure main says app.js not index.js or server.js

{
  "name": "stack-test",
  "version": "1.0.0",
  "main": "app.js",
  "license": "MIT",
  "dependencies": {
    "express": "^4.16.2"
  }
}

This will give you an err if something breaks.

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

var port = process.env.PORT || 3000;

app.get('/', function(req, res) {
  res.send("Hi there");
})


app.listen(port, (err) => {
  if (err) {
    console.log(`Error: ${err}`);
  } else {
    console.log(`App running on port ${port}`);
  }
});