
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
knex-router
Advanced tools
Small and to the point: use this Express router if you want a light way to have your database exposed as a JSON Restful API
For example:
GET https://my.site/knex/users could return [{id: 1, name: 'John'}, {id: 2, name: 'Sue'}]GET https://my.site/knex/users/2 would return {id: 2, name: 'Sue'}DELETE https://my.site/knex/users/1 would remove John (user with ID 1) from your database.npm i --save knex-router
Asumming the usual express and Knex settings, you would have to do something like this:
// import the module
const knexRouter = require("./knexRouter");
// import and set knex up
const knex = require("knex")(...settings...);
// use knex-router as a middleware, providing the knex instance
app.use("/knex", knexRouter({ knex: knex }));
This router provides the four basic DML operations (CRUD, or create, read, update and delete) through the HTTP verbs GET, POST, PUT and DELETE.
Invoking
GET https://my.site/knex/users
will perform
SELECT * FROM users
and return the result as a JSON array.
Invoking
GET https://my.site/knex/users/33
will perform
SELECT * FROM users WHERE id = 33
and return the result as a JSON object.
Invoking
POST https://my.site/knex/users
will perform
INSERT INTO users (col1, col2) VALUES (1, 2)
using as values the ones provided in the HTTP POST request body.
Invoking
PUT https://my.site/knex/users/47
will perform
UPDATE users SET col1 = 'ab', col2 = 'cd' WHERE id = 47
using as values the ones provided in the HTTP request body.
Invoking
DELETE http://my.site/knex/users/83
will perform
DELETE FROM users WHERE id = 83
and return the result Knex gives as JSON.
IMPORTANT: All this routes assume that the route name is the same as the table name, and that the primary key column is named id (if this isn't the case it can be customized, see next section)
By default all requests handle the errors with console.error and sending a response status HTTP 500, and the error object as JSON.
If your primary key is not called id -as the module uses by default- you can specify its name, on each call, with the URL query param idColumn. For example, if you call
GET https://mysite.com/knex/users/3?idColumn=user_id
it would yield the following SQL query:
SELECT * FROM users where user_id = 3
If you want to secure this router's routes, you can add any security middleware.
app.use("/knex", authMiddleware, knexRouter({ knex: app.knex }));
Build your own, this is supposed to be small and simple!
All routes have implemented a cache preventing mechanism. No cache here!
@luispablo
FAQs
Expose your knex managed database through Restful API Express routes
We found that knex-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.