Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

User-created routes ExpressJS

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);

})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading