代码拉取完成,页面将自动刷新
同步操作将从 bailee/lite 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
var express = require('express');
var app = express();
var http = require('http');
//设置环境变量
var session = require('express-session');
app.use(session({ 'secret': 'CoCo-secret', 'resave': false, 'name': 'CoCoName', 'saveUninitialized': true,'cookie': {'secure': false}}));
//express 的cookie的解析组件
var cookieParser = require('cookie-parser');
app.use(cookieParser('CoCo-secret'));
//express 的http请求体进行解析组件
var bodyParser = require('body-parser');
app.use(bodyParser['urlencoded']({ 'limit': '50mb', 'extended': false }));
app.use(bodyParser['json']({ 'limit': '50mb' }));
//文件上传模块
var multipart = require('connect-multiparty');
app.use(multipart({ 'uploadDir': './data/upload_tmp'}));
//压缩组件,需要位于 express.static 前面,否则不起作用
var compress = require('compression');
app.use(compress());
app.set('views', __dirname + '/build');
app.set('view engine', 'ejs');
//数据库
var DB = require("./server/lib/database.js");
//设置静态资源路径
app.use('/', express.static('build'));
app.use('/', express.static('data'));//用户数据内容
//系统配置文件
var AppConfig=require('./config.js')
//全局变量
global.dirname = __dirname;
//启动http(80端口)==================================
http.createServer(app).listen(8080, '0.0.0.0', function () { console.log('HTTP APP started on port http://localhost:8080'); });
//平台总入口
app.all('*', function (req, res, next) {
console.log('当前请求:'+ req.method +"="+ req.url);
if ((req.session['userid'] == undefined) && req.signedCookies['userid']) {
req.session['userid'] = req.signedCookies['userid'];
req.session['username'] = req.signedCookies['username'];
req.session['nickname'] = req.signedCookies['nickname'];
//判断系统管理员权限:此处写死,无需从数据库获取
req.session['is_admin'] = 0;
if (AppConfig.admin_list.indexOf(req.session['username']) != -1){
req.session['is_admin'] = 1;
}
}
if (req.session['userid']) {
res.locals['login'] = true;
res.locals['userid'] = req.session['userid'];
res.locals['username'] = req.session['username'];
res.locals['nickname'] = req.session['nickname'];
res.locals['is_admin'] = req.session['is_admin'];
} else {
res.locals['login'] = false;
res.locals['userid'] = '';
res.locals['username'] = '';
res.locals['nickname'] = '';
res.locals['is_admin'] = 0;
}
next();
});
//首页
app.get('/', function (req, res) {
//获取已分享的作品总数:1:普通作品,2:推荐的优秀作品
var SQL = `SELECT `+
` (SELECT count(id) FROM scratch WHERE state>0 ) AS scratch_count, `+
` (SELECT count(id) FROM python WHERE state>0 ) AS python_count `
DB.query(SQL, function(err, data){
if (err){
// console.error('数据库操作出错:');
res.locals.scratch_count = 0;
res.locals.python_count = 0;
} else {
res.locals.scratch_count = data[0].scratch_count;
res.locals.python_count = data[0].python_count;
}
// 获取首页广告
SQL = `SELECT id, content FROM ads WHERE state=1 ORDER BY i ASC`;
DB.query(SQL, function (err, ADS) {
if (err) {
console.error(err);
ADS = [];
}
res.locals['ads'] = encodeURIComponent(JSON.stringify(ADS));
res.render('ejs/index.ejs');
});
});
});
//翻页:Scratch作品列表:数据
app.post('/index/getScratchProjects', function (req, res) {
var curr = parseInt(req.body.curr); //当前要显示的页码
var limit = parseInt(req.body.limit); //每页显示的作品数
var SQL = `SELECT id, title, state FROM scratch WHERE state>0 ORDER BY view_count DESC LIMIT ${(curr-1)*limit}, ${limit}`;
DB.query(SQL, function (err, data) {
if (err) {
res.status(200).send([]);
} else {
res.status(200).send(data);
}
});
});
//翻页:Python作品列表:数据
app.post('/index/getPythonProjects', function (req, res) {
var curr = parseInt(req.body.curr); //当前要显示的页码
var limit = parseInt(req.body.limit); //每页显示的作品数
var SQL = `SELECT id, title, state FROM python WHERE state>0 ORDER BY view_count DESC LIMIT ${(curr-1)*limit}, ${limit}`;
DB.query(SQL, function (err, data) {
if (err) {
res.status(200).send([]);
} else {
res.status(200).send(data);
}
});
});
//搜索:Scratch项目列表:数据//只搜索标题
app.post('/index/seachProjects', function (req, res) {
if (!req.body.txt){
res.status(200).send([]);
return;
}
var tabelName = 'scratch';
if (req.body.t == 'p'){
tabelName = 'python';
}
var SQL = `SELECT id, title FROM ${tabelName} WHERE state>0 AND (title LIKE ?) LIMIT 12`;
var WHERE = [`%${req.body.txt}%`];
DB.qww(SQL, WHERE, function (err, data) {
if (err) {
res.status(200).send([]);
} else {
res.status(200).send(data);
}
});
});
//放在最后,确保路由时能先执行app.all=====================
//注册、登录等功能路由,含密码找回功能
var router_register = require('./server/router_user.js');
app.use('/user', router_register);
//个人中心路由//学生平台路由
var router_admin = require('./server/router_my.js');
app.use('/my', router_admin);
//系统平台路由
var router_admin = require('./server/router_admin.js');
app.use('/admin', router_admin);
//scratch路由
var router_scratch = require('./server/router_scratch.js');
app.use('/scratch', router_scratch);
//python路由
var router_python = require('./server/router_python.js');
app.use('/python', router_python);
//广告系统
var router_ads = require('./server/router_ads.js');
app.use('/ads', router_ads);
//放在最后,友好的处理地址不存在的访问
app.all('*', function (req, res, next) {
res.locals.tipType = '访问错误';
res.render('ejs/404.ejs');
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。