node js POST mysite/uploads 404(未找到)上传到 azure 但在本地工作
node js POST mysite/uploads 404 (Not Found) when uploaded to azure but works locally
我正在为一个小网页开发一个上传项目,我让它在本地运行,我可以上传文件,我可以操作它们,我可以获得必要的响应数据。我昨晚把整个事情都推到了 azure 上,现在当我尝试上传一些东西时,我得到了
POST mysite/uploads 404 (Not Found)
我的file.html
<form id="uploadForm"
enctype="multipart/form-data" action="/uploads" method="post">
<input type="file" name="userPhoto" class="btn btn-wire" />
<input type="submit" value="Upload" name="submit" class="btn btn-primary" />
</form>
html-file.js
jQuery(document).ready(function () {
$('#uploadForm').submit(function () {
$("#status").empty().text("File is uploading...");
$(this).ajaxSubmit({
error: function (xhr) {
$("#status").text('Error: ' + xhr.status);
//alert(JSON.stringify(xhr));
},
success: function (response) {
// var test = JSON.stringify(response[1], null, 4);
// console.log(JSON.stringify(response, null, 4));
files = [];
$(response).each(function (index) {
files.push(response[index]);
})
refreshFiles();
alert("alertsuccess");
$("#status").empty().text("File is uploaded...");
}
});
return false;
});
});
nodeapp.js
app.use(multer({
dest: "./uploads",
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
values = JSON.stringify(file);
console.log(values);
test.push({ "url": file.path, "filename": file.name, "fileType": file.extension });
}
}));
app.post('/uploads'), function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.end("Error uploading the file!");
}
else {
//res.json({ "test": "test123" });
res.json(test)
// res.write(JSON.stringify(test, null, 4));
res.end();
test = [];
}
})
})
还有很多代码,但为了简单起见以及我对项目的这个特定部分的普遍怀疑,我只发布了这些片段。
如何部署到 Azure Web Apps?由于 Azure Web Apps 上的 Node.js 应用程序 运行 通过 IISNode 托管在 IIS 上。因此,在 IIS 上配置您的应用程序需要一个 web.config
文件。
您可以检查您网站根目录下的这个文件,是否配置正确。
这是一个 web.config
的示例:
<configuration>
<system.webServer>
<handlers>
<!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
<add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<!-- All other URLs are mapped to the Node.js application entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
</rewrite>
<!-- You can control how Node is hosted within IIS using the following options -->
<!--<iisnode watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade"/>-->
</system.webServer>
</configuration>
我正在为一个小网页开发一个上传项目,我让它在本地运行,我可以上传文件,我可以操作它们,我可以获得必要的响应数据。我昨晚把整个事情都推到了 azure 上,现在当我尝试上传一些东西时,我得到了
POST mysite/uploads 404 (Not Found)
我的file.html
<form id="uploadForm"
enctype="multipart/form-data" action="/uploads" method="post">
<input type="file" name="userPhoto" class="btn btn-wire" />
<input type="submit" value="Upload" name="submit" class="btn btn-primary" />
</form>
html-file.js
jQuery(document).ready(function () {
$('#uploadForm').submit(function () {
$("#status").empty().text("File is uploading...");
$(this).ajaxSubmit({
error: function (xhr) {
$("#status").text('Error: ' + xhr.status);
//alert(JSON.stringify(xhr));
},
success: function (response) {
// var test = JSON.stringify(response[1], null, 4);
// console.log(JSON.stringify(response, null, 4));
files = [];
$(response).each(function (index) {
files.push(response[index]);
})
refreshFiles();
alert("alertsuccess");
$("#status").empty().text("File is uploaded...");
}
});
return false;
});
});
nodeapp.js
app.use(multer({
dest: "./uploads",
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
values = JSON.stringify(file);
console.log(values);
test.push({ "url": file.path, "filename": file.name, "fileType": file.extension });
}
}));
app.post('/uploads'), function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.end("Error uploading the file!");
}
else {
//res.json({ "test": "test123" });
res.json(test)
// res.write(JSON.stringify(test, null, 4));
res.end();
test = [];
}
})
})
还有很多代码,但为了简单起见以及我对项目的这个特定部分的普遍怀疑,我只发布了这些片段。
如何部署到 Azure Web Apps?由于 Azure Web Apps 上的 Node.js 应用程序 运行 通过 IISNode 托管在 IIS 上。因此,在 IIS 上配置您的应用程序需要一个 web.config
文件。
您可以检查您网站根目录下的这个文件,是否配置正确。
这是一个 web.config
的示例:
<configuration>
<system.webServer>
<handlers>
<!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
<add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<!-- Don't interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js\/debug[\/]?" />
</rule>
<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<!-- All other URLs are mapped to the Node.js application entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
</rewrite>
<!-- You can control how Node is hosted within IIS using the following options -->
<!--<iisnode watchedFiles="*.js;node_modules\*;routes\*.js;views\*.jade"/>-->
</system.webServer>
</configuration>