Last active
January 13, 2019 09:04
-
-
Save maishsk/9d51f46a1dc48d67c75bd4d3fb26d085 to your computer and use it in GitHub Desktop.
Dockerfile
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
FROM node:8 | |
# Create app directory | |
WORKDIR /usr/src/app | |
# Install app dependencies | |
# A wildcard is used to ensure both package.json AND package-lock.json are copied | |
# where available (npm@5+) | |
COPY package*.json ./ | |
RUN npm install | |
# If you are building your code for production | |
# RUN npm install --only=production | |
# Bundle app source | |
COPY . . | |
EXPOSE 3000 | |
CMD [ "npm", "start" ] |
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
{ | |
"name": "docker_web_app", | |
"version": "1.0.0", | |
"description": "Node.js on Docker", | |
"main": "server.js", | |
"scripts": { | |
"start": "node server.js" | |
}, | |
"dependencies": { | |
"express": "^4.16.4", | |
"winston": "3.1.0", | |
"mysql": "2.16.0" | |
} | |
} |
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
const express = require('express') | |
mysql = require('mysql') | |
const app = express() | |
const port = 3000 | |
let winston = require('winston'); | |
let logger = winston.createLogger({ | |
level: 'info', | |
format: winston.format.combine( | |
winston.format.timestamp(), | |
winston.format.printf(info => { | |
return `${info.timestamp} ${info.level}: ${info.message}`; | |
}) | |
), | |
transports: [ | |
new winston.transports.Console(), | |
new winston.transports.File({filename: 'app.log'}) | |
] | |
}); | |
var connection = mysql.createConnection( | |
{ | |
host : '192.168.106.130', | |
user : 'root', | |
password : 'my-secret-pw', | |
database : 'maishsk', | |
} | |
); | |
app.get('/hello', (request, response) => { | |
connection.query('SELECT name FROM mytable WHERE id=1 LIMIT 1', (error, results, fields) => { | |
if (error) { | |
return console.error(error.message); | |
} | |
logger.log('info', results[0].name); | |
console.log(results[0].name); | |
return response.json(results) | |
}); | |
}); | |
app.listen(port, (err) => { | |
if (err) { | |
return console.log('something bad happened', err) | |
} | |
console.log(`server is listening on ${port}`) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment