Welcome to the Linux Foundation Forum!

Can you say if this lab is correct?

Posts: 21
edited September 2022 in LFW212 Class Forum

const express = require('express');
const app = express();
const model = require('./model');
const createError = require('http-errors')

app.use((req, res, next) => {
if (req.method !== 'GET') {
next(createError(405))
} else {
next()
}
})

app.get('/boat/:id', (req, res) => {

  1. model.boat.read(req.params.id, (err, data) => {
  2.  
  3. if (err) {
  4.  
  5. if (err.code == 'E_NOT_FOUND') {
  6. res.sendStatus(404);
  7. } else {
  8. res.sendStatus(500);
  9. }
  10.  
  11. } else {
  12. res.send(data);
  13. }
  14.  
  15. })

});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(Server started on port ${PORT} - Lab 5.1 - Implement a RESTful JSON GET));

module.exports = app

This discussion has been closed.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training