标签:code core demo script r.js UI IV str use
Nodejs 应用简单的访问Mongodb 部署至Docker
原文地址 https://www.cnblogs.com/zhangyanbo/p/5851644.html (自己操作时遇到不能继续的情况,所以稍做了修改完善)
.
├── app.js
├── controller
├── Dockerfile
├── model
├── node_modules
├── package.json
├── router
└── schemas
schemas/user.js
var mongoose = require(‘mongoose‘)
var UserSchema = new mongoose.Schema({
name:{
unique:true,
type:String
},
password:{
type:String
},
role:{
type:Number,
default:5
}
})
UserSchema.statics = {
fetch:function(cb){
return this.find({})
.exec(cb)
},
findById:function(id,cb){
return this.findOne({_id:id}).exec(cb)
}
}
module.exports = UserSchema
model/user.js
var mongoose = require(‘mongoose‘)
var UserSchema = require(‘../schemas/user‘)
var User = mongoose.model(‘User‘,UserSchema);
module.exports = User;
controller/user.js
var User = require(‘../model/user‘)
exports.list = function(req,res){
User.fetch(function(err,users){
if(err) {console.log(err)}
res.send(users);
})
}
exports.save = function(req,res){
console.log(req.query.name);
var name = req.query.name || ‘noname‘;
var password = ‘abcdefg‘;
var user = new User({"name":name,"password":"123456"})
user.save(function(err){
if(err)
{
console.log(‘false‘);
res.send("fails");
return;
}
res.redirect(‘/‘);
})
}
./app.js
var express = require(‘express‘);
var router = express.Router();
var mongoose = require(‘mongoose‘)
//var dbURL = ‘mongodb://localhost:27017/imooc‘;
var dbURL = ‘mongodb://mongodb/imooc‘;//mongodb这个名字是 在--link=aaa:bbb时的名字 即bbb
mongoose.connect(dbURL);
var app = express();
app.listen(3000,function(){
console.log("server start. Listen port:3000");
});
require(‘./router/router‘)(app)
router/router.js
var User = require(‘../controller/user‘)
var _ = require(‘underscore‘)
module.exports = function(app){
app.get(‘/‘,User.list)
app.get(‘/user‘,User.save);
}
确保本地能够正常运行。
$ npm install express mongoose underscore
FROM node
RUN mkdir -p /home/src
COPY . /home/src
RUN cd /home/src; npm install --registry=https://registry.npm.taobao.org
EXPOSE 3000
CMD ["node", "/home/src/app.js"]
进入终端
cd /home
mkdir db
mongod --dbpath=‘/home/db‘
启动mongodb服务
6.docker build -t nodejsdemo ./
docker run -it -p 3000:3000 --name nodejsdemo --link=mongodb:mongodb modejsdemo
此时控制台可以看到 server start Listen port:3000
浏览器访问 192.168.99.100:3000
docker & nodejs & mongodb Nodejs 应用简单的访问Mongodb 部署至Docker
标签:code core demo script r.js UI IV str use
原文地址:https://www.cnblogs.com/musiq66/p/9146101.html