9951 explained code solutions for 126 technologies


expressjsHow can I use Knex with Express.js to query a database?


Knex is an SQL query builder for Node.js that can be used with Express.js to query a database. To use Knex with Express, first install the Knex and the appropriate database client library (i.e. pg for PostgreSQL).

npm install knex pg

Then, create a Knex instance and connect it to the database.

const knex = require('knex')({
  client: 'pg',
  connection: {
    host : '127.0.0.1',
    user : 'your_database_user',
    password : 'your_database_password',
    database : 'myapp_test'
  }
});

After the connection is established, you can use Knex to query the database. For example, to select all records from a table named users, you can use the knex.select() method:

knex.select().from('users')
  .then(rows => {
    for (row of rows) {
      console.log(`${row['name']} is ${row['age']}`);
    }
  });

Output example

John is 25
Jane is 23

The knex.select() method takes in a list of fields to select, a where clause to filter results, and other parameters to customize the query. You can find more information about the knex.select() method in the Knex documentation.

Once the query is executed, the results can be used in Express routes and views. For example, the results of the query above can be used to render a list of users in an Express view:

app.get('/users', (req, res) => {
  knex.select().from('users')
    .then(rows => {
      res.render('users', { users: rows });
    });
});

In summary, Knex can be used with Express.js to query a database. To use Knex, install the Knex and database client libraries, create a Knex instance and connect it to the database, then use the knex.select() method to query the database. The results can then be used in Express routes and views.

Edit this code on GitHub