Node.js Node include upload.js给了我一个错误连接
我刚刚使用node.js 当服务器页面为/signup时,我想调用另一个.js 所以我有我的server.js:Node.js Node include upload.js给了我一个错误连接,node.js,node-modules,Node.js,Node Modules,我刚刚使用node.js 当服务器页面为/signup时,我想调用另一个.js 所以我有我的server.js: const http = require('http') const port = 80 const ip = 'localhost' const server = http.createServer((req, res) => { if (req.url == '/') { res.end('<h1>Home</h1>'); }
const http = require('http')
const port = 80
const ip = 'localhost'
const server = http.createServer((req, res) => {
if (req.url == '/') {
res.end('<h1>Home</h1>');
}
else if (req.url == '/signup') {
require('./upload.js');
}
})
server.listen(port, ip, () => {
console.log(`running http://${ip}:${port}`)
console.log('Press ctrl + c to stop');
})
consthttp=require('http'))
常数端口=80
常量ip='localhost'
const server=http.createServer((req,res)=>{
如果(req.url=='/'){
res.end(“家”);
}
else if(req.url='/signup'){
需要('./upload.js');
}
})
侦听(端口,ip,()=>{
log(`running http://${ip}:${port}`)
log('按ctrl+c停止');
})
和upload.js:
var formidable = require('formidable');
var fs = require('fs');
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
var newpath = '/' + files.filetoupload.name;
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
});
});
}
else {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<form action="fileupload" method="post" enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
var-nervouse=require('nervouse');
var fs=需要('fs');
如果(req.url='/fileupload'){
var form=new.IncomingForm();
解析(请求、函数(错误、字段、文件){
var oldpath=files.filetoupload.path;
var newpath='/'+files.filetoupload.name;
fs.rename(旧路径、新路径、函数(err){
如果(错误)抛出错误;
res.write('上传并移动文件!');
res.end();
});
});
}
否则{
res.writeHead(200,{'Content-Type':'text/html'});
res.write(“”);
res.write(“
”);
res.write(“”);
res.write(“”);
返回res.end();
}
问题是/注册给了ERR\u CONNECTION\u拒绝
怎么了?有什么想法吗?
谢谢 问题在于您对NodeJ的思考方式。您不需要文件来调用/执行该文件(尽管它们会被执行)。您要求它在当前文件中包含其他文件的功能。然后执行所需的功能。很像#include in C。我希望您阅读以了解它在nodejs中是如何工作的 此外,如果您还没有阅读过nodejs中异步编程的概念,那么您必须理解它-