[英]How to use multiple request handling functions with Node.js Express?
[英]Node.js how to use this and call functions
在IntelliJi中,創建以下node.js應用程序:
該應用程序將包括兩個文件。 students.js文件和app.js文件。
在students.js文件中,將存在以下模塊:
changeStudentInfo-調用時會將學生姓名,學生編號,學生地址,學生所在城市,學生所在州和學生郵政編碼存儲到可以由另一個函數訪問的變量中。 信息從app.js程序傳遞到函數中。 提示:觀看“此指針”視頻以及“模塊”視頻。
displayStudentInfo-將顯示在changeStudentInfo函數中設置的變量的內容。
app.js文件將:
調用displayStudentInfo函數以顯示每個變量的DEFAULT值。
調用changeStudentInfo函數將變量信息更改為您的信息。 (您可以使用假地址)。
調用displayStudentInfo函數以顯示更改的值。
這是我對學生檔案的最了解
var info = {
name: "Default",
major: "Major",
sum: function(){
console.log(this.name);
console.log(this.major);
}
};
function changeStudentInfo() {
this.name = "Kita";
this.address = "CandyLane";
}
function displayStudentInfo(){
info.sum();
}
module.exports.studentinfo = displayStudentInfo;
module.exports.studentupdate = changeStudentInfo;
這是我在應用程序文件中的內容
var student = require('./student');
student.studentinfo();
student.studentupdate();
更改為此以修改學生信息-
function changeStudentInfo(name, address) {
info.name = name;
info.address = address;
}
然后像這樣從應用程序文件調用此函數-
student.studentupdate('Kita', 'CandyLane');
在應用程序文件中執行此操作-
var student = require('./student');
student.studentinfo();
student.studentupdate('Kita', 'CandyLane');
student.studentinfo();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.