Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Express Basics Deeper into Routing with Express The Response Object

My solution using pure nodeJS

Just did this for fun. Take a look. Took me a minute to realize the post is returning a URL and not just the query string. hence the require 'url' along with the require 'querystring'

const Express = require('express');
const app = Express();
const querystring = require('querystring');
//app.use(Express.urlencoded({ extended: true}))
app.set('view engine', 'pug');
const port = 3000;

app.get('/', (req, res) => {
  res.render('index', {prompt: "Guess a number", hint: "1-10"});
});

app.get('/hello', (req, res) => {
  res.render('hello');
});
//unsafe POST request in pure nodejs
app.post('/hello', (req, res) => {
  let body = '';
  let result
  req.on('data', (chunk) => {
      body += chunk.toString();
  }).on('end', () => {
    result = querystring.parse(body);
    res.render('hello', {username: result.username});
  })
});

app.listen(port, () => {
  console.log(`Listening at http://localhost:${port}`);
});

1 Answer

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Nice job, It's always a fun learning experiment to try and recreate things done in libraries that pull away all that abstraction from you. Great way to understand what's going on underneath the hood and as such a better way to gain more knowledge in the field subset.

Thank you, I could not agree more. I always enjoy a good rabbit hole. XD