Could use some help with configuring express routes with parameters

Greetings! I am new to working with Express and I am currently in the process of creating a portfolio website. In my project, I have a Pug file named project.pug which includes HTML content that should dynamically render based on the ID of each project stored in my JSON file:

"data": {

    "projects": [
        {
            "id": "0",
            "project_name": " ",
            "description": " ",
            "technologies": [ " "],

        },
        {
            "id": "1",
            "project_name": " ",
            "description": " ",
            "technologies": [ " "],

        },

I am currently working on setting up a route where the URL contains an :id parameter to display content specific to each project's ID. For instance, if I visit http://localhost:8000/project/1, I expect to see details related to project 1. Similarly, by navigating to http://localhost:8000/project/2, information about project 2 should be displayed.

router.get('/project:id', (req, res) => {
res.render('project');
req.app.locals = data.projects.id;
const { id } = req.params //this variable represents the id of the project
const { side } = req.query; //this variable represents the page loaded with each project's info

However, I am facing challenges in implementing the JavaScript logic required to achieve this dynamic content rendering. Any assistance or guidance would be greatly appreciated!

Answer №1

To accomplish this task, implementing a loop is essential:

for (let item of data.projects){
  if(item.id === id){
    res.status(200).json(item);
  } else {
    res.status(500).json({message: "Sorry, there are no projects matching this ID"});
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The query encoding function of the NextJS router

Encountering an issue while trying to open a new page using NextJS router. The parameter was passed as follows: router.push({ pathname: '/', query: { id: '12344567' }, }) Occasionally, the page redirects to something like: /%3Fid ...

Is the CSS scale activated by mouseover or click?

My CSS code successfully scales images, but the issue is that it affects every image on the page. I am looking for a solution to apply this CSS only when the user hovers over or clicks on an image. The challenge is that images are added by non-technical w ...

Elegant Bootstrap 4 Carousel featuring a glimpse of the upcoming slide alongside the primary carousel item

I am in search of a straightforward Bootstrap 4 carousel that showcases a glimpse of the next slide on the right. Despite exploring similar questions, I have not found a suitable solution. The links to those questions are: 1)Bootstrap carousel reveal part ...

How to update an Array<Object> State in ReactJS without causing mutation

In my program, I store an array of objects containing meta information. This is the format for each object. this.state.slotData [{ availability: boolean, id: number, car: { RegistrationNumber : string, ...

Explain to me the process of passing functions in TypeScript

class Testing { number = 0; t3: T3; constructor() { this.t3 = new T3(this.output); } output() { console.log(this.number); } } class T3 { constructor(private output: any) { } printOutput() { ...

Ways to achieve combined outcomes using ng-repeat

Check out this plunker. <div ng-repeat="subCategory in subCategorys | filter:{tags:tag}:true | orderBy:'id'"> {{subCategory.id}} {{subCategory.name}} {{subCategory.tags}} <br/><br/> The detailed information of ...

Troubleshooting Problems with Cookie and Session Management

I'm encountering an issue while trying to set cookies and session values during login on the backend, which is built in node js. However, when react calls the API to verify these cookies or session values, they are returning as undefined... My middle ...

Reset input fields upon jQuery removal

Currently, I am working on a form that includes a remove function. The function is functioning correctly, but I am facing an issue where I want the field values to be cleared when the remove function is triggered. This is necessary as I am sending input va ...

Consolidate and then transfer to a different database

How can I insert the data returned from my aggregate (controller function) into another collection called User? Here is the schema for the User: const userSchema = new mongoose.Schema({ firstName: { type: String, required: [true, &ap ...

Setting up Node on CentOS

I am currently in the process of setting up node on my CentOS system running version 2.6.31.11-an_centos6-v42 #3 SMP Thu Oct 31 12:17:24 PDT 2013 i686 i686 i386 GNU/Linux. After downloading the tar file from nodejs.org, I followed these steps: tar -xf no ...

React-native - Dropdownpicker - Error: Unable to retrieve label for selected choice

I'm encountering an issue with DropDownPicker in my react-native project during page load Here is the code snippet where I am using DropDownPicker: <DropDownPicker items={[ { la ...

Disposing of memory in THREE JS when switching between routes in VUE

Currently, I am delving into the world of VUE JS and working on a basic SPA that navigates through different pages. In my spare time, I have developed several THREE JS demos which unfortunately tend to slow down and eventually halt when switching between ...

Error: The Vuex store is not defined in the Vue.js component when attempting to access it using

I've recently set up a new vue-cli webpack project with Vuex. I have initialized my Vuex store in store.js like so: import Vue from "vue"; import Vuex from "vuex"; Vue.use(Vuex); const store = new Vuex.Store({ state: {} }); export default store; ...

Steps for deploying an npm project to Heroku

Currently, I have an npm project created with vue-cli and a socket.io server. This is the structure of my project: Project/ |--node_server/ | |--server.js |--src/ | |--main.js | |--App.vue | |--other .vue files and folders I am facing difficulties d ...

Ways to refresh UI in ReactJS without triggering a specific event

In my React application, I am displaying various pictures and GIFs that users can attach to a post. Currently, each image has an onClick handler which triggers either a modal with different options or deletes the picture if the user holds down the ctrl key ...

Tips for splitting the json_encode output in Javascript

Looking for help with extracting specific values from JSON data retrieved via PHP and AJAX. I only want to display the agent name in my ID, not the entire object that includes "name" : "Testing". Here is the console output: [{"agent_module_id":"1","agen ...

Exploring the potential of incorporating Bower into the Openshift Django framework

I am currently running an app in Openshift with Python 2.7 and Mysql 5.5 cartridges, and I am looking to deploy a Django app on it. To get started with building the app, I followed the tutorial provided here. Locally, I use bower as my package manager, wh ...

Incorporating an SVG with CSS styling from a stylesheet

After exploring various articles and questions on the topic, I am yet to find a definitive solution. I have an external file named icon.svg, and my objective is to utilize it across multiple HTML files with different fill colors and sizes for each instanc ...

Trouble with pinch zoom functionality in a Vue component

I have a Vue component that allows me to zoom in on an image using the mouse wheel, but I'm experiencing strange behavior with pinch zoom. When I place two fingers far apart on the screen, it zooms in significantly, and when I bring them closer togeth ...

Ways to implement a percentage for scrollTop

I'm currently troubleshooting the code below. My goal is to understand why I'm unable to scroll the .post div with jQuery and input range properly using a percentage value like this: $("[type=range]").on('input',function(){ var v ...