问题
I'm working on an upload project for a small webpage and I made it all work locally, I can upload files, I can manipulate them and I can get necessary data in response. I pushed the whole thing on azure last night and now when I try to upload something I get
POST mysite/uploads 404 (Not Found)
My 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 = [];
}
})
})
There's quite a bit more code but for the sake of simplicity and my general suspiciousness on this particular part of project I posted only these snippets.
回答1:
How do you deployment to Azure Web Apps? As Node.js applications running on Azure Web Apps are hosted on IIS via IISNode. So there is a web.config
file required to config your application on IIS.
You may check this file in your root directory of your site, whether it is in the correct configurations.
Here is a sample of a 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>
来源:https://stackoverflow.com/questions/35558099/node-js-post-mysite-uploads-404-not-found-when-uploaded-to-azure-but-works-loc