Working with Nodejs and Strapi

System Information
  • Strapi Version: 4.11.5
  • Operating System: Win/Mac
  • Database: SQLite
  • Node Version: 18
  • NPM Version:
  • Yarn Version:

I don’t know if I am going about this in all the wrong way.

I have a folder with my node js app in and another folder with strapi in

myNodeApp
  >node_modules
  >models
  >controllers
  >routers
  >views
  app.js
  ...etc

myNodeAppStrapi
  >.cache
  >.tmp
  >build
  >node_modules
  ...etc

I am able to fetch data from the localhost:1337 server, using quite horrible code.

await fetch('http://127.0.0.1:1337/api/questions?populate[options][populate][0]=icon')

I don’t like the need to populate, feel it should be configurable at source.

I see that there other more declarative methods available for fetching data that strapi provides e.g.

strapi.db.query('api::blog.article').findMany({ // uid syntax: 'api::api-name.content-type-name'
  where: {
    title: {
      $startsWith: '2021',
      $endsWith: 'v4',
    },
  },
  populate: {
    category: true,
  },
});

I do not know how I access these features from myNodeApp. Where or what to import to access this functionality.

Is having separate folders wrong?

Am I barking up the wrong tree trying to build an app in Node JS using Strapi for the backend? I don’t see many tutorials. Most are React/Next etc.


I was past the time for editing my last post, but regards the fetch query and populate, I see that could be simplified to

await fetch('http://localhost:1337/api/questions?populate=options.icon')

After initial frustrations I have made some progress.

Installing strapi-sdk-js in the frontend, combined with the Transformer plugin in the strapi backend has made a big difference.

A simple config in plugins.js to flatten the response data removing data and attributes.

module.exports = ({ env }) => ({
 'transformer': {
    enabled: true,
    config: {
      responseTransforms: {
        removeAttributesKey: true,
        removeDataKey: true
      }
    }
  }
});

I’m now able to get clean data to populate my templates with.

const questions = await strapi.find('questions/1',{
  fields: ['title', 'template'],
  populate: {
    'header_icon': {
      fields: ['url', 'alternativeText']
    },
    'options': {
      fields: ['text_en', 'text_cy'],
      populate: {
        'icon': {
          fields: ['url', 'alternativeText']
        }
      }
    }
  }
})

without all the noise

"data": {
    "id": 1,
    "title": "Who do you live",
    "template": "single-choice.ejs",
    "header_icon": {
      "id": 14,
      "url": "/uploads/...",
      "alternativeText": "..."
    },
    "options": [
      {
        "id": 1,
        "text_en": "a friend",
        "text_cy": "ffrind",
        "icon": {
          "id": 4,
          "url": "/uploads/...",
          "alternativeText": "..."
        }
      },
      ...

}

Still very much open to suggestions, but feeling a bit more positive now moving forwards :+1:

Again can I ask, how do I access this sort of functionality from my front end folder? Basically how are you doing this? Where are you running this type of code from? Locally in the strapi folder itself?

Do I need to include strapi from the backend node_modules folder? e.g. …/backend/node_modules/

Driving me to despair as I cannot find any decent documentation that gives the full picture in plain English in how to go about this. I am kind of wondering where this secret resource of information is.

Every guide I have seen so far seems to presume prior knowledge and gives a small window into the solution.

Just create your own controller /api/article/your-custom-controller (in Strapi) and add the quoted code there.

@lolafkok, unfortunately for me and possibly other people new to strapi it’s not a case of ‘just do this… or just create that…’.

I know from helping people out on a particular web development forum, you have to remind yourself sometimes what it was like when you were starting out.Things that are simple to you aren’t for others.

About 5 minutes after venting on this forum, I did find this video and it’s all making a bit more sense now.

I was working with an MVC pattern in my frontend folder, I guess a lot of the business logic can now be done in the strapi backend folder instead.

Is that the way to do things? I do want to get this right, and it’s a lot of guesswork at the moment.