Skip to content

Commit

Permalink
add upload images
Browse files Browse the repository at this point in the history
  • Loading branch information
dotos committed Oct 23, 2016
1 parent 1c9a274 commit f03b088
Show file tree
Hide file tree
Showing 13 changed files with 507 additions and 131 deletions.
51 changes: 39 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,46 @@ var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var sassMiddleware = require('node-sass-middleware');
var routes = require('./routes/index');
var users = require('./routes/users');
var index = require('./routes/index');
var weibo = require('./routes/weibo');
var request = require('superagent');
// 设置与安全相关的HTTP头的中间件
var helmet = require('helmet');
//
var passport = require('passport');
// 定时器
var schedule = require('node-schedule');

function scheduleCancel() {
var counter = 1;
var t = schedule.scheduleJob('* * * * * *', function() {
console.log('定时器触发次数:' + counter);
counter++;
});
setTimeout(function() {
console.log('定时器取消!');
t.cancel();
}, 5000);
}
scheduleCancel();
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.enable('trust proxy');
// 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(cookieParser('sefaalsfNLjKXklasflnNLKNJLJFNlnknlkjfsLFSN'));
// 启用 passport 组件
app.use(passport.initialize());
app.use(passport.session());
// 启用 helmet
app.use(helmet());
//sass
//app.use(sassMiddleware({
// src: __dirname
Expand All @@ -28,10 +54,11 @@ app.use(cookieParser());
// , debug: true
//}));
app.use('/static', express.static(path.join(__dirname, 'static')));
app.use('/', routes);
app.use('/users', users);
app.use('/', index);
app.use('/weibo', weibo);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
Expand All @@ -40,21 +67,21 @@ app.use(function (req, res, next) {
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message
, error: err
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message
, error: {}
message: err.message,
error: {}
});
});
module.exports = app;
5 changes: 5 additions & 0 deletions bin/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var cluster = require('cluster');
cluster.setupMaster({
exec: 'worker.js',
slient: true
});
85 changes: 85 additions & 0 deletions bin/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
let app = require('../app');
let debug = require('debug')('API:server');
let http = require('http');
const cluster = require('cluster');

/**
* Get port from environment and store in Express.
*/
console.log(`[worker ${cluster.worker.id}] start ...`);
let port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

let server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
let 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;
}

let 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() {
let addr = server.address();
let bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port;
debug('Listening on ' + bind);
}
185 changes: 102 additions & 83 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,106 @@
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('Bing:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
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);

/**
* 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);
}
let app = require('../app');
let debug = require('debug')('API:server');
let http = require('http');
const cluster = require('cluster');
const cpus = require('os').cpus().length;
if (cluster.isMaster) {
console.log('[master] ' + "start master...");

for (var i = 0; i < cpus; i++) {
cluster.fork();
}

cluster.on('listening', function(worker, address) {
console.log('[master] ' + 'listening: worker' + worker.id + ',pid:' + worker.process.pid + ', Address:' + address.address + ":" + address.port);
});
cluster.on('exit', function(worker, code, signal) {
console.log(`[master] pid:${worker.id} is died, restarting...`);
cluster.fork();
});
} else {
console.log('[worker] ' + "start worker ..." + cluster.worker.id);
/**
* Get port from environment and store in Express.
*/

let port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

let server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
let 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;
}

let 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() {
let addr = server.address();
let bind = typeof addr === 'string' ?
'pipe ' + addr :
'port ' + addr.port;
debug('Listening on ' + bind);
}
}
11 changes: 11 additions & 0 deletions configs/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
weibo: {
CLIENT_ID: '1833831541',
CLIENT_SECRET: '204239c8f3e37e59f2d3eb95de2811a9',
ACCESS_TOKEN: '',
MASTER_ACCESS_TOKEN: '',
MASTER_COOKIES: 'SINAGLOBAL=5484186899582.633.1473772704217; _s_tentry=-; Apache=8980261524863.621.1477197913275; ULV=1477197913448:7:6:1:8980261524863.621.1477197913275:1477139933649; login_sid_t=752b624fceb9dc2cec36edd0d52acd85; UOR=,,login.sina.com.cn;SCF=Aq48LQxPt1qE63U0qWHsK8bxbvP8_cVQf74lq_fySeLLzKXgR7mwyGoBhtIa2qiADjaddlKAknPi4EuETiGbCb0.; SUB=_2A251CN2sDeTxGeNG4lEX9S3LyDqIHXVWfEhkrDV8PUNbmtBeLRHWkW8Dew228qirbEm8w5khzGUa21ZcsQ..;SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5WEmHY08N.cY1WYIGoLoRU5JpX5K2hUgL.Fo-R1KecSKeNe0q2dJLoIEBLxKMLB.eLBKBLxKMLB.2LBoMLxK-LBKeL1h2LxKMLB.2LBont; SUHB=0d4uo5w4_YKfuy; ALF=1508761979; SSOLoginState=1477225980; [email protected]; wvr=6',
MASTER_UID: '5893653736',
USER_UID: ''
}
}
Loading

0 comments on commit f03b088

Please sign in to comment.