我正在尝试使用Docker部署我的节点应用程序。这是我的节点应用程序:
var express = require('express');
var path = require('path');
var mysql = require('mysql');
var app = express();
var bodyparser = require('body-parser');
var mongoose =require('mongoose');
app.use(express.static("./app"));
var pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'root',
database: 'node_db'
});
app.set('port',8080);
app.set('views',path.join(__dirname,'views'));
app.set('view engine','jade');
app.use(express.static(path.join(__dirname,'public')));
app.use(bodyparser.urlencoded({ extended: false }));
app.get('/', function (req, res) {
res.sendfile('app/index.html');
});
app.get('/getAllBlogs', function (req, res) {
pool.getConnection(function (err, connection) {
var sql = mysql.format("select * from blogs");
connection.query(sql, function (error, results, fields) {
connection.release();
if (error) {
res.send(error);
}
res.send(results);
});
});
});
app.get('/getBlog/:id', function(req,res){
console.log(req.params.id);
pool.getConnection(function (err, connection) {
var sql = mysql.format("select * from blogs where id=?", [req.params.id]);
connection.query(sql, function (error, results, fields) {
connection.release();
if (error) {
res.status(200).send(error);
}
res.send(results[0])
});
});
});
app.listen(app.get('port'));
console.log('~~Server Runnign on port localhost:'+app.get('port')+'~~');
这是我的Docker文件:
FROM node:boron
# Create app directory
RUN mkdir -p /home/sameera/Desktop/test/app
WORKDIR /home/sameera/Desktop/test/app
# Install app dependencies
COPY package.json /home/sameera/Desktop/test/app
RUN npm install
# Bundle app source
COPY . /home/sameera/Desktop/test/app
EXPOSE 8080
CMD [ "npm", "start" ]
docker文件已成功构建,当我在命令下运行时,它正在正常运行。
docker run -p 49160:8080 img
但是当我尝试加载一个包含使用mysql的数据的页面时 给出错误:
connection.query(sql, function (error, results, fields) {
^
TypeError: Cannot read property 'query' of undefined
}
我想问题是因为我无法使用此容器连接到mysql,因为我没有安装或使用docker文件中的任何mysql相关命令。当我删除所有我的sql连接和相关代码时,它不会给任何错误并正常运行。我正在使用ubuntu 14.04
答案 0 :(得分:0)
您正在使用池连接到MySQL并通过连接进行查询。
使用池进行查询:
var mysql = require('mysql');
var pool = mysql.createPool({ connectionLimit : 10, host : 'example.org', user : 'bob', password : 'secret', database : 'my_db' });
pool.query('SELECT 1 + 1 AS solution', function (error, results, fields) { if (error) throw error; console.log('The solution is: ', results[0].solution); });
答案 1 :(得分:0)
如果不需要游泳池,您可以使用以下代码:
var express = require('express');
var path = require('path');
var mysql = require('mysql');
var app = express();
var bodyparser = require('body-parser');
var mongoose = require('mongoose');
app.use(express.static("./app"));
var pool = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'node_db'
});
pool.connect();
app.set('port', 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyparser.urlencoded({ extended: false }));
app.get('/test', function (req, res) {
var sql = mysql.format("SELECT * FROM users");
pool.query(sql, function (error, results, fields) {
if (error) {
res.send(error);
}
res.send(results);
pool.end();
});
});
app.listen(app.get('port'));
console.log('Server Runnign on port localhost:' + app.get('port'));
结果会是这样的:
[
{
"id": 1,
"firstName": "shubham",
"lastName": "verma",
"userName": "shubham",
"email": "shubham@email.com",
"password": "25d55ad283aa400af464c76d713c07ad",
"sessionId": "bdfb43ae-ee9e-4819-b35a-3cb254885023",
"isLive": 1,
"date": "2017-05-06T06:05:24.000Z"
}
]
但是如果池是必要的并且你想使用池,那么你应该尝试下面的代码:
var express = require('express');
var path = require('path');
var mysql = require('mysql');
var app = express();
var bodyparser = require('body-parser');
var mongoose = require('mongoose');
app.use(express.static("./app"));
var pool = mysql.createPool({
connectionLimit: 10,
host: 'localhost',
user: 'root',
password: 'root',
database: 'node_db'
});
app.set('port', 8080);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyparser.urlencoded({ extended: false }));
app.get('/test', function (req, res) {
pool.getConnection(function (err, connection) {
var sql = mysql.format("SELECT * FROM users");
connection.query(sql, function (error, results, fields) {
if (error) {
res.send(error);
}
res.send(results);
connection.release();
});
});
});
app.listen(app.get('port'));
console.log('Server Runnign on port localhost:' + app.get('port'));
结果会是这样的:
[
{
"id": 1,
"firstName": "shubham",
"lastName": "verma",
"userName": "shubham",
"email": "shubham@email.com",
"password": "25d55ad283aa400af464c76d713c07ad",
"sessionId": "bdfb43ae-ee9e-4819-b35a-3cb254885023",
"isLive": 1,
"date": "2017-05-06T06:05:24.000Z"
}
]