Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Commit

Permalink
sample project for Node Js + Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
kmehant committed Jun 17, 2019
1 parent 3d6210a commit 3d0da7f
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions containers/javascript-node-lts-postgres/test-project/server.js
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}`);

0 comments on commit 3d0da7f

Please sign in to comment.