如何解决在远程服务器中使用 nodejs 应用程序
我正在尝试使用 nodejs 上传文件。这是我在本地尝试的代码。
var http = require('http');
var express = require("express");
var fs = require('fs');
var formidable = require('formidable');
var upload_html = fs.readFileSync("upload_file.html");
var upload_path = "./uploads/";
http.createServer(function (req,res) {
if (req.url == '/uploadform') {
res.writeHead(200);
res.write(upload_html);
return res.end();
} else if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req,function (err,fields,files) {
var oldpath = files.filetoupload.path;
var newpath = upload_path + files.filetoupload.name;
fs.rename(oldpath,newpath,function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
});
});
}
}).listen(8086);
<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
<style>
body{text-align:center;}
form{display:block;border:1px solid black;padding:20px;}
</style>
</head>
<body>
<h1>Upload files to Node.js Server</h1>
<form action="fileupload" method="post" enctype="multipart/form-data">
<input type="file" name="filetoupload">
<input type="submit" value="Upload">
</form>
</body>
</html>
现在,我想将其推送到远程服务器。我已经有服务器的IP。
有人可以解释一下要遵循的必要步骤以及要对代码进行哪些更改。
我尝试将文件复制到服务器,但是当我尝试运行 node index.js
时它不起作用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。