Skip to content

Commit 1dcbae2

Browse files
Add javascript/express and typescript/next examples
1 parent 648c8d8 commit 1dcbae2

File tree

23 files changed

+2252
-0
lines changed

23 files changed

+2252
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# connection string for chinook.db on sqlitecloud.io
2+
CHINOOK_URL="sqlitecloud://user:password@host.sqlite.cloud:8860/chinook.db"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Getting Started
2+
3+
First, run the development server:
4+
5+
```bash
6+
npm run start
7+
```
8+
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.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var createError = require('http-errors')
2+
var express = require('express')
3+
var path = require('path')
4+
5+
var tracksRouter = require('./routes/index')
6+
7+
var app = express()
8+
9+
app.use(express.json())
10+
app.use(express.urlencoded({ extended: false }))
11+
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))
18+
})
19+
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')
29+
})
30+
31+
module.exports = app
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('with-javascript-express:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

0 commit comments

Comments
 (0)