简体   繁体   中英

Programatically add paths to Express in nodeJS

I was wondering if there was a way to dynamically(not sure if the right word) add paths to nodeJS. What I mean is that, I want to allow the user to add some more paths, from within the running program. A kludged version would be to simply have the routes in a file, and use a NPM library that resets the server when it notices that files in the app have changed (I forgot what the NPM packet was called). That isn't really a viable way for even a semi-production ready kinda thing.

Any ideas?

EDIT: The weekend project this is for is a server as a service. Imagine you are prototyping and app, or taking part of a hackathon, and you need a server with a database to store your data. The problem at this point could be that you don't know server side, or that you don't have the time to set it up. So what you could do with this project is, specify table/database names, and the paths for GET, POST, PUT, DELETE, and what kinds of data you'd want to put in, and it would give you the actual URL and any authentication stuff.

So the question is, when a user wants to add a path, how can i actually do that?

The way I picture it working is as follows:

POST on /apps with body {name: "myFirstDB", fields: [{name: "id", type: "String"}, {name: "age", type: Integer}] sets up a database for my user (normanjoyner) named myFirstDB with the fields: id and age. In the body of the express routing, you create a new mongoDB collection using the specified schema.

POST on /apps/:username/:dbname with body {id: "1", age: 21} will add an entry to the database. (Could also accept array of objects to add multiple entries at once)

PUT on /apps/:username/:dbname?id=1 with body {id: "1", age: 22} will update specific entries matching query string params (note that the query string param should be a unique id if you only intend to update one entry)

GET on /apps/:username/:dbname returns all entries for specified db

GET on /apps/:username/:dbname?id=1 returns only entries matching query string params

DELETE on /apps/:username/:dbname will delete the entire db and schema

DELETE on /apps/:username/:dbname?id=1 will delete only certain rows which match query string criteria

You get the idea. You can certainly expand on this and hopefully meet all of your needs.

Hope this helps!

If you are looking for an implementation of the use case, take a look at Json Feed Server . It does most of what you intend of your app.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM