This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sample project for Node Js + Postgres
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
containers/javascript-node-lts-postgres/test-project/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*-------------------------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. | ||
*-------------------------------------------------------------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
const express = require('express'); | ||
const promise = require('bluebird'); | ||
|
||
// Constants | ||
const PORT = 3000; | ||
const HOST = '0.0.0.0'; | ||
|
||
var result; // stores the connection result | ||
|
||
const initOptions = { | ||
promiseLib: promise // overriding the default (ES6 Promise); | ||
}; | ||
const pgp = require('pg-promise')(initOptions); | ||
|
||
|
||
|
||
// Database connection details; | ||
const cn = { | ||
host: 'db', // host of db container | ||
port: 5432, // 5432 is the default; | ||
database: 'data', // database name | ||
user: 'user', // database user name | ||
password: 'pass' // database password | ||
}; | ||
|
||
const db = pgp(cn); // database instance; | ||
|
||
db.one(' SELECT current_database();') | ||
.then( | ||
result = "Successfully connected to database." | ||
) | ||
.catch(error => { | ||
result = "Failed: " + error | ||
}) | ||
|
||
// creating an express app | ||
const app = express(); | ||
app.get('/', async (req, res) => { | ||
res.send('Hello, remote world: ' + result); | ||
}); | ||
app.listen(PORT, HOST); | ||
console.log(`Running on http://${HOST}:${PORT}`); | ||
|