Welcome to the Linux Foundation Forum!

Lab 3.1 - Deliver Data from a Library API

First, the assert equal is deprecated.

When I processed the data.js file, I got a string, but the assert test compare against an object.

I did check the type of the values and they were different so, I checked the values and they were the same in “content” and “data”. To get the assert ok y convert to an object or test other assertions and it worked.

Adding this to compare:
${HOST} must respond with result calling data lib function - ${content} && ${data} ${content == data} /=/ ${typeof(content)}, ${typeof(data)}

results:
⛔️ http://localhost:3000 must respond with result calling data lib function - kbo7Nfn1ii12kQ== && kbo7Nfn1ii12kQ== true
➜ Lab-3.1 node validate.js
⛔️ http://localhost:3000 must respond with result calling data lib function - EJcq50G8foy5og== && EJcq50G8foy5og== true
➜ Lab-3.1 node validate.js
⛔️ http://localhost:3000 must respond with result calling data lib function - IN/yrkDdzbKZjw== && IN/yrkDdzbKZjw== true
➜ Lab-3.1 node validate.js
☑️ GET http://localhost:3000/ responded with data output
☑️ GET http://localhost:3000/example responded with 404 Not Found status

Comments

  • hey @canelacho

    assert.equal is deprecated, assert.strict.equal is not, notice at the top of validate.js const assert = require('assert').strict

    the assert is comparing against a buffer object, the bug is that content needs to be converted to a string. Thanks for letting us know!

  • hey @davidmarkclements

    Referring to Lab-3.1,

    validate.js call randomBytes(10).toString('base64') to get data.
    my server.js call randomBytes(10).toString('base64') seperately to get data.

    How can both data be the same?

  • @chesterheng said:
    hey @davidmarkclements

    Referring to Lab-3.1,

    validate.js call randomBytes(10).toString('base64') to get data.
    my server.js call randomBytes(10).toString('base64') seperately to get data.

    How can both data be the same?

    Same issue here.

  • Hi @tayuelo I am not sure if I can post my code for lab-3.1 here for review.

    Except Lab-3.1, I managed to validate all labs. At lab 8 now.

  • @chesterheng said:
    Hi @tayuelo I am not sure if I can post my code for lab-3.1 here for review.

    Except Lab-3.1, I managed to validate all labs. At lab 8 now.

    Wow, I really want to validate this lab, but I just don't know why are those strings different. Hope @davidmarkclements helps me with this asap.

  • I wanted too but skip for now to continue.
    Should review each other answers?
    If posting answer here is not allow, I can message you.

  • To understand how the data can be the same, see line 23, 25, and 28 of validate.js

    @tayuelo can you please post the code that you're unable to validate

  • Hi @davidmarkclements,

    My code as below. Please help me to review. Thank you.

    index.js
    const express = require('express');
    const router = express.Router();
    const data = require('../data');

    router.get('/', async function(req, res, next) {
    res.send(await data());
    });

    module.exports = router;

    app.js
    'use strict'
    const express = require('express');
    const createError = require('http-errors');
    const indexRouter = require('./routes/index');

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

    const app = express();

    app.use('/', indexRouter);

    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
    next(createError(404));
    });

    app.use((err, req, res, next) => {
    res.status(err.status || 500);
    res.send(err.message);
    });

    app.listen(port);

  • davidmarkclementsdavidmarkclements Posts: 122
    edited December 2020

    @chesterheng your code should pass as long as you have setup the start script in the package.json correctly.

    This is mentioned in the training but it's important to emphasize that the following is unsafe in Express:

    router.get('/', async function(req, res, next) {
      res.send(await data());
    });
    

    Express has no concept of async functions or promises, this route handler will return a promise. If for any reason the promise returned from the data async function was to reject, this would in turn cause the route handler function to reject - but Express does not handle promises returned from route handler functions, and therefore does not handle the rejection.

    By default this will cause an unhandled promise rejection, putting your service into an unknown state. It can also leads to memory leaks.

    I would advise that you don't use async functions with Express, but if you do you should wrap a try/catch around the entire function body:

    router.get('/', async function(req, res, next) {
      try {
        res.send(await data());
      } catch (err) {
        next(err)
      }
    });
    

    That way any rejections are propagated via Express and the error handling middleware.

  • Hi @davidmarkclements,

    Thank you for review my code.

    I still cannot validate lab3-1.
    ⛔️ http://localhost:3000 must respond with result calling data lib function

  • your code seems to work:

    did you perhaps modify the validate.js file at all?

Sign In or Register to comment.