restructure repo, separate sample app from docker configs
This commit is contained in:
25
05-example-web-application/api-node/src/db.js
Normal file
25
05-example-web-application/api-node/src/db.js
Normal file
@ -0,0 +1,25 @@
|
||||
const { Pool } = require('pg');
|
||||
|
||||
const pool = new Pool();
|
||||
|
||||
// the pool will emit an error on behalf of any idle clients
|
||||
// it contains if a backend error or network partition happens
|
||||
pool.on('error', (err, client) => {
|
||||
console.error('Unexpected error on idle client', err);
|
||||
process.exit(-1);
|
||||
});
|
||||
|
||||
// async/await - check out a client
|
||||
const getDateTime = async () => {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const res = await client.query('SELECT NOW() as now;');
|
||||
return res.rows[0];
|
||||
} catch (err) {
|
||||
console.log(err.stack);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { getDateTime };
|
||||
28
05-example-web-application/api-node/src/index.js
Normal file
28
05-example-web-application/api-node/src/index.js
Normal file
@ -0,0 +1,28 @@
|
||||
const { getDateTime } = require('./db');
|
||||
|
||||
const express = require('express');
|
||||
const morgan = require('morgan');
|
||||
|
||||
const app = express();
|
||||
const port = 3000;
|
||||
|
||||
// setup the logger
|
||||
app.use(morgan('tiny'));
|
||||
|
||||
app.get('/', async (req, res) => {
|
||||
const dateTime = await getDateTime();
|
||||
const response = dateTime;
|
||||
response.api = 'node';
|
||||
res.send(response);
|
||||
});
|
||||
|
||||
const server = app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`);
|
||||
});
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
debug('SIGTERM signal received: closing HTTP server');
|
||||
server.close(() => {
|
||||
debug('HTTP server closed');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user