我是node.js的新手,并且我试图在数据库保存后更新视图,我应该使用vue / react来做吗?
修改
我有我的bin / www文件;
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('demo:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '8000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Socket.io
**/
var io = app.io;
io.listen(server);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
然后是我的app.js文件;
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var hbs = require('express-handlebars');
var https = require('https');
var socket_io = require('socket.io');
var app = express();
// view engine setup
app.engine('hbs', hbs({extname: 'hbs', defaultLayout: 'layout', layoutsDir: __dirname + '/views/layouts'}));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
//Socket setup
var io = socket_io();
app.io = io;
var index = require('./routes/index')(io);
// io.on('connection', function(socket){
// console.log('made socket connection', socket.id);
// //io.sockets.emit('data', socket.id);
// });
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/js', express.static(__dirname + '/node_modules/bootstrap/dist/js')); // redirect bootstrap JS
app.use('/js', express.static(__dirname + '/node_modules/jquery/dist')); // redirect JS jQuery
app.use('/css', express.static(__dirname + '/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap
app.use('/', index);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
我的index.js文件;
module.exports = function(io) {
var express = require('express');
var router = express.Router();
var cron = require('node-cron');
var apiCalls = require('../apiCalls');
var livedata = [];
io.on('connection', function(socket){
console.log('made socket connection', socket.id);
io.sockets.emit('data', socket.id);
io.sockets.emit('data', livedata);
});
router.get('/', function(req, res, next) {
res.render('index', { title: 'demo', data: 'yey!' });
});
var cronJob = cron.schedule('*/3 * * * * *', function(){
apiCalls.getData(function(data){
livedata.push(data);
console.log(data);
});
});
cronJob.start();
var empty_arr = cron.schedule('*/11 * * * * *', function(){
livedata = [];
});
empty_arr.start();
return router;
}
我试图使用cron工作进行api调用,该工作正常。我想通过使用socket io每秒更新视图。这也很好。当我加载localhost:8000时,套接字打印连接到控制台。
我试图将这些结合起来2.由于我是学习node.js的先驱,我想到了将数据推送到数组然后从socket.io调用该数组的想法。
然后我每隔11秒清空一次数组。问题是,为了能够看到数据,我仍然需要刷新页面。我的理解是,如果我必须刷新页面以查看数据,为什么要使用socket.io。
我认为我做错了,我希望它会自动刷新。
谢谢。