Skip to content

Commit 26def96

Browse files
Update examples/with-javascript-express
1 parent 8553e30 commit 26def96

File tree

12 files changed

+316
-200
lines changed

12 files changed

+316
-200
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# connection string for chinook.db on sqlitecloud.io
2-
CHINOOK_URL="sqlitecloud://user:password@host.sqlite.cloud:8860/chinook.db"
1+
# connection string for your database node on sqlitecloud.io
2+
DATABASE_URL="sqlitecloud://user:password@host.sqlite.cloud:8860"
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
## Getting Started
22

3-
First, run the development server:
3+
This example shows how to use @sqlitecloud/drivers in a simple Javascript express http server.
4+
5+
To run the example:
46

57
```bash
8+
npm install
69
npm run start
710
```
811

9-
Open [http://localhost:3000/api/hello](http://localhost:3000) with your browser to see the result.
10-
11-
You can start editing the API route by modifying `routes/index.js`.
12-
13-
## Learn More
14-
15-
To learn more about Next.js, take a look at the following resources:
16-
17-
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
18-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
12+
Open [http://localhost:3000/](http://localhost:3000/) with your browser to see the result.
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1-
var createError = require('http-errors')
2-
var express = require('express')
3-
var path = require('path')
1+
//
2+
// Using @sqlitecloud/drivers inside an Express app with Javascript
3+
//
44

5-
var tracksRouter = require('./routes/index')
5+
var sqlitecloud = require('@sqlitecloud/drivers')
6+
require('dotenv').config()
7+
var DATABASE_URL = process.env.DATABASE_URL
8+
console.assert(DATABASE_URL, 'DATABASE_URL environment variable not set in .env')
69

10+
var express = require('express')
11+
var http = require('http')
712
var app = express()
8-
913
app.use(express.json())
10-
app.use(express.urlencoded({ extended: false }))
1114

12-
app.use('/', tracksRouter)
13-
app.use('/api/hello', tracksRouter)
14-
15-
// catch 404 and forward to error handler
16-
app.use(function (req, res, next) {
17-
next(createError(404))
15+
/* http://localhost:3001/ returns chinook tracks as json */
16+
app.get('/', async function (req, res, next) {
17+
var database = new sqlitecloud.Database(DATABASE_URL)
18+
var tracks = await database.sql`USE DATABASE chinook.sqlite; SELECT * FROM tracks LIMIT 20;`
19+
res.send({ tracks })
1820
})
1921

20-
// error handler
21-
app.use(function (err, req, res, next) {
22-
// set locals, only providing error in development
23-
res.locals.message = err.message
24-
res.locals.error = req.app.get('env') === 'development' ? err : {}
25-
26-
// render the error page
27-
res.status(err.status || 500)
28-
res.render('error')
22+
const port = process.env.PORT || 3000
23+
var server = http.createServer(app)
24+
server.listen(port, () => {
25+
console.log(`Server is running on http://localhost:${port}/`)
2926
})
3027

3128
module.exports = app

examples/with-javascript-express/bin/www

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)