Question about the req object

Hi folks, a quick questions I hope you can help me with.

I am studying express.js and node.js and I am reviewing the X-press publishing project toward the end of the full stack developer path.

Can you explain me why in the first code snippet is being used while in the second code snippet req.artist is used instead?

code snippet 1'/', (req, res, next) => {
    const name =;
    const dateOfBirth = req.body.artist.dateOfBirth;
    const biography = req.body.artist.biography;
    const isCurrentlyEmployed = req.body.artist.isCurrentlyEmployed === 0 ? 0 : 1;

    if (!name || !dateOfBirth || !biography) {
        return res.sendStatus(400);

    const sql = 'INSERT INTO Artist (name, date_of_birth, biography, is_currently_employed) ' +
                'VALUES ($name, $dateOfBirth, $biography, $isCurrentlyEmployed)';
    const values = { $name: name,
                     $dateOfBirth: dateOfBirth,
                     $biography: biography,
                     $isCurrentlyEmployed : isCurrentlyEmployed};, values, function(error) {
        if (error) {
        } else {
            db.get(`SELECT * FROM Artist WHERE = ${this.lastID}`,
            (error, artist) => {
                res.status(201).json({ artist: artist });

code snippet 2

artistsRouter.param('artistId', (req, res, next, artistId) => {
    const sql = 'SELECT * FROM Artist WHERE = $artistId';
    const values = {$artistId: artistId};

    db.get(sql, values, (error, artist) => {
        if (error) {
        } else if (artist) {
            req.artist = artist;
            next();                 // passing it to the next middleware
        } else {

In what does artist stand for? I don’t get where it comes from.

Thank you in advance =).

Hello, welcome back to the forums

The first code snippet is accessing JSON data that was attached to the body of the API request when the front-end made a request. You’re able to access it like this because of the body-parser middleware that you implemented in server.js.

The second code snippet is attaching the data to the req object after retrieving information from the database, so that the routes that use the :artistId parameter won’t have to retrieve it again. They would be able to access it on req.artist such as

In this case, artist is the name of the object property that the front-end sent the data on. You can check out how the front-end that was provided is doing it here: src/utils/XPress.js Here’s a snippet of the code that makes a request to the API in your first code snippet

XPress.createArtist = artist => {
  const url = `${baseUrl}/artists`;
  const fetchOptions = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    body: JSON.stringify({artist: artist})
   ... // then all the fetch code

Here you’ll notice body: JSON.stringify({artist: artist}) is setting the name that you use in your code.

1 Like

Thank you @selectall for taking the time to answer my question!