当上传到azure时,节点js POST mysite /上传404(未找到),但在本地工作 [英] node js POST mysite/uploads 404 (Not Found) when uploaded to azure but works locally

查看:115
本文介绍了当上传到azure时,节点js POST mysite /上传404(未找到),但在本地工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个小型网页上传一个上传项目,并且我在本地完成了所有工作,我可以上传文件,我可以操纵它们,并且可以获得必要的数据。我昨天晚上把所有的东西都推到了azure上,现在当我尝试上传我得到的东西时

  POST mysite / uploads 404(Not找到)

我的文件.html

 < form id =uploadForm
enctype =multipart / form-dataaction =/ uploadsmethod =post>
< input type =filename =userPhotoclass =btn btn-wire/>
< input type =submitvalue =Uploadname =submitclass =btn btn-primary/>
< / form>

html-file.js
<$ p $ ()函数(){
$(#status())$ j $('#uploadForm')。 ).empty()。text(File is uploading ...);
$(this).ajaxSubmit({
error:function(xhr){
$(#)状态)。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。原始名称+'正在开始...');
},
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 uploadloading 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

 <配置> 
< system.webServer>
< handlers>
<! - 表示app.js文件是由iisnode模块处理的node.js应用程序 - >
< / handlers>
< rewrite>
< rules>

<! - 不要干涉节点检查器调试请求 - >
< rule name =NodeInspectorpatternSyntax =ECMAScriptstopProcessing =true>
< / rule>

<! - 首先我们考虑传入的URL是否与/ public文件夹中的物理文件匹配 - >
< rule name =StaticContent>
< action type =Rewriteurl =public {REQUEST_URI}/>
< / rule>

<! - 所有其他URL都映射到Node.js应用程序入口点 - >
< rule name =DynamicContent>
<条件>
< add input ={REQUEST_FILENAME}matchType =IsFilenegate =True/>
< / conditions>
< action type =Rewriteurl =server.js/>
< / rule>

< / rules>
< / rewrite>
<! - 您可以使用以下选项控制节点在IIS内的托管方式 - >
<! - < /system.webServer>
< / configuration>


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.

解决方案

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>  

这篇关于当上传到azure时,节点js POST mysite /上传404(未找到),但在本地工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆