我试图在我的express项目中使用CryptoJS API,所以我试图在客户端调用CryptoJS API,因为我将对用户输入进行加密
在我看来,我似乎没有以正确的方式将文件发送到服务器,这就是我感到困惑的地方,因为我一直在执行app.get并尝试从指定的目录中获取文件。但是它一直找不到目录,这使我感到困惑,因为我知道该目录存在。
代码
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
users = {};
server.listen(3000);
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.use("/js", express.static(__dirname + "/js"));
app.use("/style", express.static(__dirname + "/style"));
app.get("/js/bower_components/crypto-js", function (req, res) {
res.sendFile(__dirname + '/crypto-js.js');
});
io.sockets.on('connection', function (socket) {
socket.on('new user', function (data, callback) {
if (data in users) {
callback(false);
} else {
callback(true);
socket.nickname = data;
users[socket.nickname] = socket
updateNames();
}
});
function updateNames() {
io.sockets.emit('usernames', Object.keys(users));
}
socket.on('send message', function (data, callback) {
var msg = data.trim();
if (msg.substr(0, 3) === '/w ') {
msg = msg.substr(3);
var ind = msg.indexOf(' ');
if (ind !== -1) {
var name = msg.substr(0, ind);
var msg = msg.substr(ind + 1);
if (name in users) {
users[name].emit('whisper', {
msg: msg,
nick: socket.nickname
});
console.log('hi');
} else {
callback('Error! Enter a valid user!');
}
} else {
callback("Error! Enter a pm");
}
} else {
io.sockets.emit('new message', {
msg: msg,
nick: socket.nickname
});
}
});
socket.on('disconnect', function (data) {
if (!socket.nickname) return;
delete users[socket.nickname];
updateNames();
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Chatty</title>
<link rel="stylesheet" href="style/style.css">
</head>
<body>
<div id="nickWrap">
<form id="setNick">
<p>Enter A Username:</p>
<p id="nickError"></p>
<input size="35" id="nickname">
<input type="submit">
</form>
</div>
<div id="contentWrap">
<div id="chatWrap">
<div id='chat'></div>
<form id='send-message'>
<input size="35" id="message">
<input type="submit">
</form>
</div>
</div>
<div id="users"></div>
</body>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="js/require.js"></script>
<script type="text/javascript">
require.config({
paths: {
'crypto-js': 'js/bower_components/crypto-js/crypto-js'
}
});
require(["crypto-js"], function (CryptoJS) {
console.log(CryptoJS.HmacSHA1("Message", "Key"));
jQuery(function ($) {
var socket = io.connect();
var $nickForm = $('#setNick');
var $nickError = $('#nickError');
var $nickBox = $('#nickname');
var $users = $('#users');
var $messageForm = $('#send-message');
var $messageBox = $('#message');
var $chat = $('#chat');
$nickForm.submit(function (e) {
e.preventDefault();
socket.emit('new user', $nickBox.val(), function (data) {
if (data) {
$('#nickWrap').hide();
$('#contentWrap').show();
} else {
$nickError.html('Name is taken! Choose a New one!');
}
});
$nickBox.val('');
});
socket.on('usernames', function (data) {
var html = '';
for (var i = 0; i < data.length; i++) {
html += data[i] + '<br/>'
}
$users.html(html);
});
$messageForm.submit(function (e) {
e.preventDefault();
socket.emit('send message', $messageBox.val(), function (data) {
$chat.append('<span class = "error"' + data + "</span><br/>");
});
$messageBox.val('');
});
socket.on('new message', function (data) {
$chat.append('<span class= "msg"<b>' + data.nick + ': </b>' + data.msg +
"<span/><br/>");
});
socket.on('whisper', function (data) {
$chat.append('<span class = "whisper"<b>' + data.nick + ': </b>' + data.msg +
"</span><br/>");
});
});
});
</script>
</html>
答案 0 :(得分:0)
您需要在代码中包含以下行,以使您的应用程序引用您的JS
文件夹/
因此,您需要替换当前的
app.use("/js", express.static(__dirname + "/js"));
为
app.use(express.static(path.join(__dirname, "js")));
希望这会有所帮助!