Welcome to the Linux Foundation Forum!

LFW212 : ch8-lab-1 - unable to validate

First of all, this chapter is only described with Fastify and I think that's a huge problem because we don't all use this framework.

I implemented the lab 1 with express but I can't validate

Any idea please ?

Comments

  • [Spoiler] The solution for express :

    var express = require('express')
    var router = express.Router()
    var got = require('got')

    router.get('/', async function (req, res) {
    const { url } = req.query

    try {
    new URL(url)
    } catch {
    return res.sendStatus(400)
    }

    try {
    const {
    statusCode,
    body,
    headers
    } = await got(url, { retry: 0 })

    res
    .set(headers)
    .status(statusCode)
    .send(body)
    } catch (e) {
    res.sendStatus(e.response ? e.response.statusCode : 500)
    }
    })

    module.exports = router

  • davidmarkclementsdavidmarkclements Posts: 109
    edited December 2020

    @luxfero both the training and the exams are about core skills, not framework choice. The fact you implemented in Express instead is proof that you'll be capable within the exam environment.

  • Is it better to use library? The code will be shorter.

    My solution for review:

    routes/index.js
    const express = require('express');
    const requestProxy = require("express-request-proxy");
    const router = express.Router();

    router.get('/', async function(req, res, next) {
    const { url } = req.query
    const proxy = requestProxy({ url });
    proxy(req, res, next);
    });

    module.exports = router;

    app.js
    var createError = require('http-errors');
    var express = require('express');
    var path = require('path');
    var cookieParser = require('cookie-parser');
    var logger = require('morgan');

    var indexRouter = require('./routes/index');

    var app = express();

    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'jade');

    app.use(logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));

    app.use('/', indexRouter);

    app.use(function(req, res, next) {
    next(createError(404));
    });

    app.use(function(err, req, res, next) {
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    res.status(err.status || 500);
    res.render('error');
    });

    module.exports = app;

  • @chesterheng you can use whatever you like, the exam is agnostic to give you freedom to choose. Your solution looks fine to me

Sign In or Register to comment.