以下のコードファイルに人がログインしたときに生成された番号をデータベース(つまりテスト)に挿入したい
ファイル:app.js
var express = require('express');
var routes = require('./routes');
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/test');
var schema = mongoose.Schema;
var app = module.exports = express.createServer();
// Configuration
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
secret: 'your secret here'
}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() {,
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
app.post('/authenticate', routes.authenticate);
app.get('/login', routes.login);
// Routes
app.get('/', routes.home);
app.post('/', routes.home_post_handler);
app.post('/putdata', routes.putdata);
app.listen(3001, function() {
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
ファイル: index.js
exports.home = function(req, res) {
res.redirect('/login');
};
exports.home_post_handler = function(req, res) {
// message will be displayed on console
console.log(req.body.message);
// send back the json of the message received via the textbox
res.json(req.body.message);
// or
//res.send(200);// send 200 for server processing complete
};
exports.login = function(req, res) {
res.render('login', {
title: 'login to your system'
});
};
exports.authenticate = function(req, res) {
if (req.body.txtlogin == req.body.txtpassword) {
var _guid = guidgenerator();
res.json(_guid);
db.posts.insert(_guid);// this is the main porblem
}
};
function guidgenerator() {
var s4 = function() {
return (((1 + math.random()) * 0x1000) | 0).tostring(16).substring(1);
};
return (s4() + s4() + s4());
}