了解 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;
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP