创建登录帐户
页面创建链接
In the views:
< p > Please click <a href="/"> getting
the other file</a>
In the router:
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Lab Class' });
});
登录帐户设置
In the views:
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css'>
<script type="application/javascript" src="/javascripts/index.js"></script>
</head>
<body onload="checkForErrors(<%= login_is_correct %>)">
<p>This is the home page for <%= title %>. Please login</p>
<form action="welcome" method="POST">
Username:<br/>
<input type="text" name="username"><br>
Password:<br/>
<input type="password" name="password">
<br/>
<input type="submit"></form>
</form>
</body>
</html>
In the router:
/* POST from form. */
router.post('/welcome', function(req, res, next) {
var username= req.body.username;
var password= req.body.password;
if (username==='hi' && password==='hi'){
res.render('welcome', { title: username, login_is_correct:true });
} else {
res.render('index', { title: 'COM3504/6504', login_is_correct:false });
}
});
In the JavaScript:
//checks if variable isLoginCorrect is set. If not set Displays an alert
function checkForErrors(isLoginCorrect){
if (!isLoginCorrect){
alert('login or password is incorrect');
}
}
什么是异步编程
•异步编程是一种能够控制程序的策略,当主线正常流动以外的事件的发生’即当事件发生时执行某些代码
•典型的示例是单击按钮
在JavaScript中如何完成?
•将异步操作存储到事件队列中
•在主线程完成处理后,队列被处理
•排队的操作尽快完成
•他们将结果归还给JavaScript环境
•4种方法:
回调(经典; 超时和间隔(延迟和重复执行; 承诺(新); 等待/异步(较新)
JSON数据结构
•JSON建立在两个结构上:
名称/值对的集合。 在各种语言中,这被认为是对象,记录,结构,字典,哈希表,keyedlist或关联数组值列表的有序列表。 在大多数语言中,这是一个数组,向量,列表或序列。
•数据通过逗号分隔
•卷曲" "支架容纳对象
•方括号{ }保持数组
•记住一个数组可以包含其他阵列