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!2 -
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?
1 -
@chesterheng said:
hey @davidmarkclementsReferring 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.
0 -
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.
0 -
@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.
0 -
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.0 -
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
0 -
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);
0 -
@chesterheng your code should pass as long as you have setup the
start
script in thepackage.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.
0 -
Thank you for review my code.
I still cannot validate lab3-1.
⛔️ http://localhost:3000 must respond with result calling data lib function0 -
your code seems to work:
did you perhaps modify the validate.js file at all?
0 -
@davidmarkclements you can post the code from the validate.js file?
0 -
I have the same problem and cannot understand why.
I only created an app.js file, and did not modify the validate.js
My package.json has the "node app.js" that starts the server.const http = require('http');
const express = require('express');
const app = express();
const data = require('./data');
const port = 3000;;const server = http.createServer(app);
app.use((req, res, next) => {
if (req.path !== '/') {
res.status(404).send('Not found');
}
next();
})app.get('/', async(req, res) => {
try {
let response = await data();
res.send(response);
} catch (err) {
res.status(500).send(err);
}});
server.listen(port, () => {
console.log(Server listening on port ${port}
);
});The only message I get from validate.js is that "⛔️ http://localhost:3000 must respond with result calling data lib function"
If I open the address on the browser I do see that random string as response.
for example: /0NqLJLyWmD9Dg==Did I miss something?
0 -
Hello everyone ... I think the common mistake is.. you have to make sure the server is not already running..
as you can see on line 28 the validate script is starting the server and intercepting the crypto import with a fake
2 -
@aarongoshine well pointed out, the validate script runs the server for you in this case
1 -
To summarize issue "⛔️ http://localhost:3000 must respond with result calling data lib function"
add blow script to you package.json (app.js is your service entry point)
"scripts": {
"start": "node app.js"
},when you run "node validate.js" , there is a magic inside call "npm start" and DO NOT call "npm start" separately by your self.
0 -
I ran into the same issue and played around with validate.js then realised that validate.js is actually starting a server for us. so, i stopped the server already running and then all tests passed. YAY!
0
Categories
- 8.8K All Categories
- 12 LFX Mentorship
- 65 LFX Mentorship: Linux Kernel
- 356 Linux Foundation Boot Camps
- 226 Cloud Engineer Boot Camp
- 69 Advanced Cloud Engineer Boot Camp
- 23 DevOps Engineer Boot Camp
- 5 Cloud Native Developer Boot Camp
- 724 Training Courses
- 14 LFC110 Class Forum
- 16 LFD102 Class Forum
- 96 LFD103 Class Forum
- 2 LFD121 Class Forum
- 55 LFD201 Class Forum
- 1 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum
- 14 LFD254 Class Forum
- 420 LFD259 Class Forum
- 78 LFD272 Class Forum
- 1 LFD272-JP クラス フォーラム
- 15 LFS200 Class Forum
- 683 LFS201 Class Forum
- LFS201-JP クラス フォーラム
- 271 LFS211 Class Forum
- 50 LFS216 Class Forum
- 23 LFS241 Class Forum
- 26 LFS242 Class Forum
- 18 LFS243 Class Forum
- 4 LFS244 Class Forum
- 7 LFS250 Class Forum
- LFS250-JP クラス フォーラム
- 103 LFS253 Class Forum
- 753 LFS258 Class Forum
- 7 LFS258-JP クラス フォーラム
- 48 LFS260 Class Forum
- 74 LFS261 Class Forum
- 6 LFS262 Class Forum
- 76 LFS263 Class Forum
- 14 LFS264 Class Forum
- 10 LFS266 Class Forum
- 8 LFS267 Class Forum
- 8 LFS268 Class Forum
- 4 LFS269 Class Forum
- 173 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- 184 LFW211 Class Forum
- 99 LFW212 Class Forum
- 875 Hardware
- 204 Drivers
- 74 I/O Devices
- 43 Monitors
- 115 Multimedia
- 204 Networking
- 98 Printers & Scanners
- 82 Storage
- 716 Linux Distributions
- 78 Debian
- 64 Fedora
- 12 Linux Mint
- 13 Mageia
- 22 openSUSE
- 125 Red Hat Enterprise
- 33 Slackware
- 13 SUSE Enterprise
- 344 Ubuntu
- 445 Linux System Administration
- 33 Cloud Computing
- 63 Command Line/Scripting
- Github systems admin projects
- 88 Linux Security
- 73 Network Management
- 105 System Management
- 45 Web Management
- 50 Mobile Computing
- 18 Android
- 19 Development
- 1.2K New to Linux
- 1.1K Getting Started with Linux
- 499 Off Topic
- 119 Introductions
- 193 Small Talk
- 19 Study Material
- 742 Programming and Development
- 237 Kernel Development
- 471 Software Development
- 898 Software
- 244 Applications
- 178 Command Line
- 2 Compiling/Installing
- 72 Games
- 313 Installation
- 19 All In Program
- 19 All In Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)