Created
January 10, 2019 21:19
-
-
Save maishsk/227d13210e9ca6b98eecd3722f7173c7 to your computer and use it in GitHub Desktop.
respond as json
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