了解npm-节点模块系统
在我们之前的获取用户输入并将其存储的示例中,一个文件只有一个文件。但是在现实生活中,我们将不得不创建多个文件以使代码简单易读。
让我们看看如何在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 nhooo </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);
});
//重定向
res.statusCode=302;
res.setHeader('Location','/');
return res.end();
});
}
res.write('<html>');
res.write('<head> <title> Hello nhooo </title> </head>');
res.write(' <body> Hello </body>');
res.write('</html>');
res.end();
});
server.listen(3000);创建一个单独的文件route.js-
将代码从createServer方法移至route.js文件。
const url = req.url;
if(url === '/'){
res.write('<html>');
res.write('<head> <title> Hello nhooo </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);
});
//重定向
res.statusCode=302;
res.setHeader('Location','/');
return res.end();
});
}
res.write('<html>');
res.write('<head> <title> Hello nhooo </title> </head>');
res.write(' <body> Hello </body>');
res.write('</html>');
res.end();在route.js文件中添加文件系统模块的导入语句,并从App.js文件中删除导入,因为我们现在不在此处使用它。
文件route.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
}将route.js文件导入App.js文件。
const routes = require('./routes');更新的App.js文件现在为-
const http = require('http');
const routes = require('./routes');
const server = http.createServer(routes);
server.listen(3000);并且route.js的更新文件如下所示-
const fs = require('fs');
const requestHandler=(req, res)=>{
const url = req.url;
if(url === '/'){
res.write('<html>');
res.write('<head> <title> Hello nhooo </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);
});
//重定向
res.statusCode=302;
res.setHeader('Location','/');
return res.end();
});
}
res.write('<html>');
res.write('<head> <title> Hello nhooo </title> </head>');
res.write(' <body> Hello </body>');
res.write('</html>');
res.end();
}
module.exports= requestHandler;