Home > database >  User-created routes ExpressJS
User-created routes ExpressJS

Time:11-22

there are websites which create custom sessions for users, giving them unique link to the exact session user has created. E.g. it would like something like https/website.com/session/UniqueRandomID. I guess I understand how custom routes in ExpressJS work, but I'm not quite sure how can I allow a user to create those and later allow other users to connect only to those which have been already created..

Is there a common way of doing it and what may I be missing on the topic?

I tried searching the expressJS documentation.

CodePudding user response:

The term "session" has a rather specific meaning in web site development (it refers to data associated with a given browser's visit to a site and is used for things like tracking the logged in state of a user) so I'll use the term "project" in this answer.

When the user creates a project, store all the information about that project in a database. Include, as part of this information an identifier. You probably want this to be a GUID or similar (there are libraries which will generates these for you) rather than something sequential (like an automatically generated database primary key).

The first page of the React Guide explains routing. Create a route that uses a route parameter for the project ID.

Use that project ID to get the data about the project from your database.

If there isn't any for that ID, return an error.

app.get('/projects/:projectId', async (req, res) => {
    const projectData = await getProjectData(req.params.projectId);
    if (projectData) {
        return res.render('projectView', projectData);
    }
    res.sendStatus(404);

})
  • Related