WildWilyWilly
WildWilyWilly

Reputation: 141

Cannot GET api route (node.js, express)

JS noob, I'm trying to follow a course on node.js API creation. I'm doing everything by the book instruction by instruction. This is the server.js file, as specified in the course:

const http = require('http');
const app = require('./app');

const normalizePort = val => {
  const port = parseInt(val, 10);

  if (isNaN(port)) {
    return val;
  }
  if (port >= 0) {
    return port;
  }
  return false;
};
const port = normalizePort(process.env.PORT || 3000);
app.set('port', port);
const errorHandler = error => {
  if (error.syscall !== 'listen') {
    throw error;
  }
  const address = server.address();
  const bind = typeof address === 'string' ? 'pipe ' + address : 'port: ' + port;
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges.');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use.');
      process.exit(1);
      break;
    default:
      throw error;
  }
};

const server = http.createServer(app);

server.on('error', errorHandler);
server.on('listening', () => {
  const address = server.address();
  const bind = typeof address === 'string' ? 'pipe ' + address : 'port ' + port;
  console.log('Listening on ' + bind);
});

server.listen(port);

I run this server from the console and cUrl and postman requests give me a 404 (Cannot GET) when I send get requests to http://localhost:3000/api/stuff, the route I specified in app.js:

const express = require('express');
const app = express();
app.use('http://localhost:3000/api/stuff', (req, res, next) => {
  const stuff = [
    {
      message: "this is a JSON"
    },
    {
      message: "this is also a JSON"
    }
  ];
  res.status(200).json(stuff);
});

My package.json seems alright:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "node": "^15.2.1"
  }
}

In app.js I tried app.get instead of app.use, and generating the JSON directly into the res.status(200).json().

I really suspect the issue is in the server.js, I tried messing around with it to no avail (feeding normalizePort() a string instead of an integer, eliminating process.env.port, simplifying the switch where it checks if port 3000 is free).

I have of course checked for similar issues in SO, but nothing seemed to match my specific problem.

node --version v13.11.0 (the course is based on an old version too)

Upvotes: 0

Views: 679

Answers (1)

xehpuk
xehpuk

Reputation: 8241

A URL consists of multiple parts:

  • scheme
  • authority (including host and port)
  • path
  • query
  • fragment

Parsing http://localhost:3000/api/stuff results in:

  • scheme: http
  • authority
    • host: localhost
    • port: 3000
  • path: /api/stuff

app.use does not expect a complete URL. It just expects the path part: app.use('/api/stuff',

You can pass more than just a simple string. Examples can be found in the documentation.

Upvotes: 1

Related Questions