瞭解 npm - Node 模組系統
在我們的獲取使用者輸入並存儲到檔案中的早期示例中,只有一個檔案。但在實際場景中,我們必須建立一個包含多個檔案的資料夾,以保持程式碼簡潔且易於閱讀。
我們來看看如何在 node.js 中使用模組系統
我們有 App.js −
const http = require('http'); const fs = require('fs'); const server = http.createServer((req, res)=>{ const url = req.url; if(url === '/'){ res.write('<html>'); res.write('<head> <title> Hello TutorialsPoint </title> </head>'); res.write(' <body> <form action="/username" method="POST"> <input type="text" name="username"/> <button type="submit">Submit</button> </body>'); res.write('</html>'); return res.end(); } if(url === '/username' && req.method === 'POST'){ const requestBody = []; req.on('data', (chunks)=>{ requestBody.push(chunks); }); return req.on('end', ()=>{ const parsedData = Buffer.concat(requestBody).toString(); const username = parsedData.split('=')[1]; fs.writeFile('username.txt', username, (error)=>{ console.log(error); }); //redirect res.statusCode=302; res.setHeader('Location','/'); return res.end(); }); } res.write('<html>'); res.write('<head> <title> Hello TutorialsPoint </title> </head>'); res.write(' <body> Hello </body>'); res.write('</html>'); res.end(); }); server.listen(3000);
建立一個單獨的檔案 routes.js −
將程式碼從 createServer 方法移動到 routes.js 檔案。
const url = req.url; if(url === '/'){ res.write('<html>'); res.write('<head> <title> Hello TutorialsPoint </title> </head>'); res.write(' <body> <form action="/username" method="POST"> <input type ="text" name="username"/> <button type="submit">Submit</button> </body>'); res.write('</html>'); return res.end(); } if(url === '/username' && req.method === 'POST'){ const requestBody = []; req.on('data', (chunks)=>{ requestBody.push(chunks); }); return req.on('end', ()=>{ const parsedData = Buffer.concat(requestBody).toString(); const username = parsedData.split('=')[1]; fs.writeFile('username.txt', username, (error)=>{ console.log(error); }); //redirect res.statusCode=302; res.setHeader('Location','/'); return res.end(); }); } res.write('<html>'); res.write('<head> <title> Hello TutorialsPoint </title> </head>'); res.write(' <body> Hello </body>'); res.write('</html>'); res.end();
在 routes.js 檔案中新增檔案系統模組的匯入語句,並從 App.js 檔案中刪除匯入,因為我們現在不在那裡使用它。
routes.js 檔案中的上述程式碼應位於 JavaScript 函式中,如下所示 −
const requestHandler = (req, res)=>{ --routes.js file code-- } get the url and method data from request is same in routes.js. const url = req.url; const method = req.method;
透過這些更改,我們可以在 routes.js 檔案中編寫一個匯出。
module.exports = requestHandler;
module.exports 可以有多個鍵值對匯出,如下所示 −
module.exports = { key1: value1, key2: value2 }
在 App.js 檔案中匯入 routes.js 檔案。
const routes = require('./routes');
更新後的 App.js 檔案現在是 −
const http = require('http'); const routes = require('./routes'); const server = http.createServer(routes); server.listen(3000);
而 routes.js 的更新後的檔案如下所示 −
const fs = require('fs'); const requestHandler=(req, res)=>{ const url = req.url; if(url === '/'){ res.write('<html>'); res.write('<head> <title> Hello TutorialsPoint </title> </head>'); res.write(' <body> <form action="/username" method="POST"> <input type="te xt" name="username"/> <button type="submit">Submit</button> </body>'); res.write('</html>'); return res.end(); } if(url === '/username' && req.method === 'POST'){ const requestBody = []; req.on('data', (chunks)=>{ requestBody.push(chunks); }); return req.on('end', ()=>{ const parsedData = Buffer.concat(requestBody).toString(); const username = parsedData.split('=')[1]; fs.writeFile('username.txt', username, (error)=>{ console.log(error); }); //redirect res.statusCode=302; res.setHeader('Location','/'); return res.end(); }); } res.write('<html>'); res.write('<head> <title> Hello TutorialsPoint </title> </head>'); res.write(' <body> Hello </body>'); res.write('</html>'); res.end(); } module.exports= requestHandler;
廣告