Wednesday, 10 May 2017

Unable to dockerize a nodeapp

Created the docker file as following:

FROM node:boron

ADD package.json package.json

RUN npm install
ADD . .

EXPOSE 4500

CMD ["node","main.js"]

Building the app:

docker build -t "appname"

Running the app:

docker run -it "appname"

Inside main.js, I have:

var express = require("express");
var app = express();
var path = require('path');
var http = require('http');
var https = require('https');
var nodemailer = require('nodemailer');
var bodyParser = require('body-parser');
var request = require("request");

var port = process.env.PORT || 3001;
app.set('port', (port));
app.listen(app.get('port'), function () {
    console.log('Node app is running on port', app.get('port'));
});

//app.use('/', express.static(path.join(__dirname, '/public_html')))
app.use('/', express.static(__dirname + '/public_html'));
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({
    limit: '5mb',
    extended: false
}));


app.get('/', function(request, response) {
  response.render('public_html/');
});

//app.get('/', function (req, res, next) {
//    res.sendFile('/index.html');
//});

app.use('*', function (req, res, err) {
//    console.log('error:  ', err);
});

when I run the app using docker command 'docker run -it "appname"`, I get the out to console:

Node app is running on port 3001

But when I browse, page is empty or nothing is loaded in the browser/output/view. It is supposed to pick up index.html from response.render('public_html/');



via kittu

No comments:

Post a Comment