我在2个单独的文件夹中及其所有关联文件中设置了2种不同的脚本。这些脚本都是相似的,并且基本上为用户执行“登录”,启动“ easyrtc服务器”,然后允许访问网页(“ chat_wip.html”),用户可以在该网页上按下按钮以建立easyrtc连接。
问题是一个脚本有效,而另一个脚本不起作用...该按钮无法在非功能脚本上建立easyrtc连接。与该脚本(与正常运行的脚本相比)的主要区别在于,更多的中间件使用和使用.ejs而非静态.html文件的页面服务。
我拼命试图找出问题所在,但是显然没有运气。我想知道是否某些node / express / ejs / middleware pro可能有一些建议...?
“良好”(功能性)代码如下:
////"Good" (functional) script:
var bodyParser = require('body-parser')
//Use Express module
var express = require('express');
var app = express();
var io = require("socket.io"); //web socket external module
var easyrtc = require("easyrtc"); //EasyRTC external module
//Statically serve files in these directories
app.use("/js", express.static(__dirname + '/easyrtc/js'));
app.use("/images", express.static(__dirname + '/easyrtc/images'));
app.use("/css", express.static(__dirname + '/easyrtc/css'));
//For my homepage
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs') //set the view engine to ejs
app.use("/css", express.static(__dirname + '/public/css'));
app.use("/img", express.static(__dirname + '/public/img'));
// Needed to parse form data
app.use(bodyParser());
//global variables
var loggedIn = false,
password = 'password'; //change to something else
var title = 'This is a heading';
var message = 'Steve is NOT available';
//Temporary home page
app.get('/', function (req, res) {
if (loggedIn == true) {
res.render('index', {title: 'available', message: 'Steve is available...'});
console.log("homepage -logged in");
}
else {
res.render('index', {title: 'NOT available', message: 'Steve is NOT available'});
console.log("homepage -not logged in");
}
});
//Serve a static login page if not logged in already
app.get('/login', function (req, res) {
console.log('Login attempt');
if (loggedIn == true) {
res.sendfile(__dirname + '/easyrtc/chat_wip.html');
}
else {
res.sendfile(__dirname + '/public/login.html')
}
});
//Respond to POST from login form
app.post('/login', function (req, res) {
console.log('post to /login');
if (loggedIn == true) {
res.send("Already logged in.");
}
else {
console.log("Posted data:" + JSON.stringify(req.body));
console.log("req.body.pword:" + req.body.pword);
console.log("req.body.usr:" + req.body.usr);
if (req.body.pword == password) {
loggedIn = true;
res.send("You are logged in.");
console.log("Logged in");
// Start EasyRTC server
var easyrtcServer = easyrtc.listen(app, socketServer);
}
else{res.send("Incorrect password.")}
}
});
//Initiate a video call
app.get('/video', function(req, res){
if (loggedIn == true) {
res.sendfile(__dirname + '/easyrtc/chat_wip.html');
}
else {
res.send("Steve is not available. Please try later.")
}
});
// Start server on port 8080
var webServer = app.listen(8080);
console.log('Listening on port ' + 80);
// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer);
如前所述,该代码虽然简单且粗糙,但是可以正常工作...我可以按“ chat_wip.html”上的按钮并启动easyrtc连接。
这是非功能代码...类似于上面的代码,但是中间件更多,.ejs而不是静态文件的使用更多:
////"Bad" (non-functional) script:
var connect = require('connect');
//File Uploads
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads')
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
});
var upload = multer({ storage: storage });
var helmet = require('helmet');
var validator = require('express-validator');
var session = require('express-session');
var cookieParser = require('cookie-parser');
var csurf = require('csurf');
var bodyParser = require('body-parser')
//Use Express module
var express = require('express');
var app = express();
var io = require("socket.io"); //web socket external module
var easyrtc = require("easyrtc"); //EasyRTC external module
app.use(helmet());
//Statically serve files in these directories
app.use("/js", express.static(__dirname + '/easyrtc/js'));
app.use("/images", express.static(__dirname + '/easyrtc/images'));
app.use("/css", express.static(__dirname + '/easyrtc/css'));
//For my homepage
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs') //set the view engine to ejs
app.use("/css", express.static(__dirname + '/public/css'));
app.use("/img", express.static(__dirname + '/public/img'));
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.use(session({
secret: 'secret-key',
key: 'secret-cookie',
resave: false,
saveUninitialized: false,
cookie: { maxAge: 60000 }
}));
var csrfProtection = csurf({ cookie: true });
//parse cookies...we need this because "cookie" is true in csrfProtection
app.use(cookieParser());
//global variables
var loggedIn = false,
password = 'password'; //change to something else
var title = 'This is a heading';
var message = 'Steve is NOT available';
var presenter = 'Username'
var passport = 'Password'
var usr, pword, data, errors
var model_name
//Temporary home page
app.get('/', csrfProtection, function (req, res) {
var site = req.hostname; //example returns "localhost"
var splits = site.split("."); //"split" on "periods"
console.log("site is: " + site);
console.log("first split is: " + splits[0]);
console.log("second split is: " + splits[1]);
if (loggedIn == true) {
res.render('index', {title: 'available', message: 'Steve is available...'});
console.log("homepage -logged in");
}
else {
console.log("homepage -not logged in");
res.render('login', {
presenter: 'username please',
passport: 'password please',
errors: {},
csrfToken: req.csrfToken()
});
}
});
//Serve a static login page if not logged in already
app.get('/login', csrfProtection, function (req, res) {
console.log('Login attempt');
if (loggedIn == true) {
res.sendfile(__dirname + '/easyrtc/chat_wip.html');
}
else {
res.render('login', {
presenter: 'username please',
passport: 'password please',
errors: {},
csrfToken: req.csrfToken()
});
}
});
//Respond to POST from login form
//POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, csrfProtection, function (req, res) {
if (!req.body) return res.sendStatus(400)
console.log("Posted data:" + JSON.stringify(req.body));
console.log("req.body.pword:" + req.body.pword);
console.log("req.body.usr:" + req.body.usr);
var usr_STR = String(req.body.usr)
var pass_STR = String(req.body.pword)
console.log("req.body.usr: " + req.body.usr + " req.body.pword: " + req.body.pword);
console.log("usr_STR: " + usr_STR + " pass_STR: " + pass_STR);
if (loggedIn == true) {
res.send("Already logged in.");
}
else {
if (req.body.pword == password) {
loggedIn = true;
console.log("Logged in");
// Start EasyRTC server
var easyrtcServer = easyrtc.listen(app, socketServer);
res.render('admin', { user_name: req.body.usr });
}
else {
res.render('login', {
presenter: 'username please',
passport: 'password please',
errors: {
presenter: {
msg: 'Your username does not look right'
},
passport: {
msg: 'Your password does not look right'
}
},
csrfToken: req.csrfToken()
});
}
}
})
//Serve an administration page if logged in already
app.get('/admin', csrfProtection, function (req, res) {
if (loggedIn == true) {
console.log('Admin Serve');
// res.send('welcome, ' + req.body.usr + ' to your administration page')
}
else {
res.render('login', {
presenter: 'username please',
passport: 'password please',
errors: {},
csrfToken: req.csrfToken()
});
}
});
//Initiate a video call
app.get('/video', function(req, res){
if (loggedIn == true) {
res.sendfile(__dirname + '/easyrtc/chat_wip.html');
}
else {
res.send("Steve is not available. Please try later.")
}
});
//Error Handling
app.use((req, res, next) => {
res.status(404).send("Sorry can't find that!")
});
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).send('Something broke!')
});
// Start server on port 8080
var webServer = app.listen(8080);
console.log('Listening on port ' + 80);
// Start Socket.io so it attaches itself to Express server
var socketServer = io.listen(webServer);
基本上就是所有这些……任何见解将不胜感激。似乎好像是在使用.ejs而不是静态.html文件服务引起了错误……或者也许是注入了“ Admin”页面……?不管是什么...它正在杀死我。先感谢您。问候。