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?
3 -
@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
3 -
@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!
1 -
@dilipagheda Exactly, we may all had this issue for this particular reason:
We followed the courses, thus there is a lot of chance that we still have a running instance of the previous server or we have created another server based on previous courses (copy-paste-edit to save time).Having the validation in its own folder and the server in another folder, the random bytes injection is not doing what the author of the course wanted to.
Please fix this or improve documentation as this is very frustrating, the injection hack is breaking the validation while the code is 100% valid.
0 -
dealing with zombie servers is a common part of microservice development, the pragmatic outcome here is everyone who hits this develops operational understanding around process management and node servers/services and most who won't hit this problem already have that understanding. Ultimately that's a feature, not a bug, this is a frequently occurring scenario in Node.js development. However it could be made clearer, a note is being added to chapter 3 to this effect.
0 -
@xdxmxc dear author of the course, the problem is not zombie servers, the problem is that the validation code is relying on a hack that makes the validation fail leaving the student thinking his code is garbage while it's 100% good.
For example, if I write the required code and launch it using my own run command and then start the validation in parallel, it fails because the random value is not shared between the app and the validation script.
A better solution would be that the validation script write to a file that is then read and returned by the app, thus passing (or not) the validation if the content is the same.
Thanks for your understanding, I had exactly this problem, I knew my code was good, but I dug and found the why, now I can just go on, but I felt it would be useful to let others know why it could not work as expected, and maybe make you change that.
0 -
fair points @jalik26 - I don't know about anyone else but I still deal with imposter syndrome (thinking my work is garbage while it's 100% good) due to a missing piece of operational knowledge. At some point you have to understand the reasons for why this would fail, which imo is good learning. However there's obviously a lot of other concerns and there's has to be a balance of tensions, and this maybe leans too far in that direction. I'll be doing a full audit and update of the material and will strongly consider your points on this as I do so. Thank you
0 -
Tips from someone just starting the course. For your validation to run successfully, make sure your
package.json
start script points to the correct file (whatever file contains your main logic).validate.js
will run it's ownnpm start
. So make sure your server is killed to avoid port conflicts.Next I was surprised to learn that Express does not support
async/await
as I see a lot of that type of code around. AFAIU a better approach is a traditional callback style:app.get( '/', ( req, res ) => { data() .then( ( result ) => { res.send( result ) } ) .catch( ( err ) => { res.status( 500 ).send( 'Internal Server Error' ); } ) } )
0 -
@louiswol94 OR you try/catch every async await function in express - you can even write a handy function to wrap it, something along the lines of:
function gaurd (fn) { return async (req, res, next) { try { await fn(req, res, next) } catch (err) { next(err) } } }
then
router.add('/some/path', gaurd(myRouteHandler))
, same for middleware etcbut it can only take you so far, Fastify being built from ground up to support this means max possible performance and you don't even have to think about it (which is the assumption, but not the truth) with Express
1
Categories
- All Categories
- 167 LFX Mentorship
- 219 LFX Mentorship: Linux Kernel
- 801 Linux Foundation IT Professional Programs
- 358 Cloud Engineer IT Professional Program
- 180 Advanced Cloud Engineer IT Professional Program
- 83 DevOps Engineer IT Professional Program
- 149 Cloud Native Developer IT Professional Program
- 112 Express Training Courses
- 138 Express Courses - Discussion Forum
- 6.2K Training Courses
- 48 LFC110 Class Forum - Discontinued
- 17 LFC131 Class Forum
- 42 LFD102 Class Forum
- 227 LFD103 Class Forum
- 19 LFD110 Class Forum
- 39 LFD121 Class Forum
- 15 LFD133 Class Forum
- 7 LFD134 Class Forum
- 17 LFD137 Class Forum
- 63 LFD201 Class Forum
- 3 LFD210 Class Forum
- 5 LFD210-CN Class Forum
- 2 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum - Discontinued
- 1 LFD233 Class Forum
- 2 LFD237 Class Forum
- 23 LFD254 Class Forum
- 697 LFD259 Class Forum
- 109 LFD272 Class Forum
- 3 LFD272-JP クラス フォーラム
- 10 LFD273 Class Forum
- 154 LFS101 Class Forum
- 1 LFS111 Class Forum
- 1 LFS112 Class Forum
- 1 LFS116 Class Forum
- 1 LFS118 Class Forum
- LFS120 Class Forum
- 7 LFS142 Class Forum
- 7 LFS144 Class Forum
- 3 LFS145 Class Forum
- 1 LFS146 Class Forum
- 3 LFS147 Class Forum
- 1 LFS148 Class Forum
- 15 LFS151 Class Forum
- 1 LFS157 Class Forum
- 34 LFS158 Class Forum
- 8 LFS162 Class Forum
- 1 LFS166 Class Forum
- 1 LFS167 Class Forum
- 3 LFS170 Class Forum
- 2 LFS171 Class Forum
- 1 LFS178 Class Forum
- 1 LFS180 Class Forum
- 1 LFS182 Class Forum
- 1 LFS183 Class Forum
- 29 LFS200 Class Forum
- 736 LFS201 Class Forum - Discontinued
- 2 LFS201-JP クラス フォーラム
- 14 LFS203 Class Forum
- 135 LFS207 Class Forum
- 1 LFS207-DE-Klassenforum
- 1 LFS207-JP クラス フォーラム
- 301 LFS211 Class Forum
- 55 LFS216 Class Forum
- 48 LFS241 Class Forum
- 48 LFS242 Class Forum
- 37 LFS243 Class Forum
- 15 LFS244 Class Forum
- LFS245 Class Forum
- LFS246 Class Forum
- 50 LFS250 Class Forum
- 1 LFS250-JP クラス フォーラム
- LFS251 Class Forum
- 155 LFS253 Class Forum
- LFS254 Class Forum
- LFS255 Class Forum
- 5 LFS256 Class Forum
- 1 LFS257 Class Forum
- 1.3K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 122 LFS260 Class Forum
- 159 LFS261 Class Forum
- 42 LFS262 Class Forum
- 82 LFS263 Class Forum - Discontinued
- 15 LFS264 Class Forum - Discontinued
- 11 LFS266 Class Forum - Discontinued
- 20 LFS267 Class Forum
- 25 LFS268 Class Forum
- 31 LFS269 Class Forum
- 3 LFS270 Class Forum
- 199 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- LFS274 Class Forum
- 3 LFS281 Class Forum
- 10 LFW111 Class Forum
- 261 LFW211 Class Forum
- 182 LFW212 Class Forum
- 15 SKF100 Class Forum
- 1 SKF200 Class Forum
- 1 SKF201 Class Forum
- 782 Hardware
- 198 Drivers
- 68 I/O Devices
- 37 Monitors
- 96 Multimedia
- 174 Networking
- 91 Printers & Scanners
- 83 Storage
- 758 Linux Distributions
- 80 Debian
- 67 Fedora
- 15 Linux Mint
- 13 Mageia
- 23 openSUSE
- 143 Red Hat Enterprise
- 31 Slackware
- 13 SUSE Enterprise
- 348 Ubuntu
- 461 Linux System Administration
- 39 Cloud Computing
- 70 Command Line/Scripting
- Github systems admin projects
- 90 Linux Security
- 77 Network Management
- 101 System Management
- 46 Web Management
- 64 Mobile Computing
- 17 Android
- 34 Development
- 1.2K New to Linux
- 1K Getting Started with Linux
- 371 Off Topic
- 114 Introductions
- 174 Small Talk
- 19 Study Material
- 806 Programming and Development
- 304 Kernel Development
- 204 Software Development
- 1.8K Software
- 263 Applications
- 180 Command Line
- 3 Compiling/Installing
- 405 Games
- 309 Installation
- 97 All In Program
- 97 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)