簡體   English   中英

在節點js中如何在提交表單時連接post方法

[英]In node js how to connect the post method while submitting form

這是我的表單,我如何在提交此表單時與節點js連接

<form id="fileupload" action="/file-upload" method="POST" enctype="multipart/form-data"> 
<input id="fileToBeUploaded" type="file" name="thumbnail">
<input type="submit" value="upload"> 
</form> 

服務器端

var http = require("http");
var url = require("url");

http.createServer(function(req, res) {
  switch (url.parse(req.url).pathname)
  { 
    case '/': console.log('server side methodcalled'); 
      break;
    case '/file-upload': console.log('server side method called');
      break; 
    default: 
      console.log('server side method called');
      break;
  }
}); 

在您的nodejs服務器程序中,您可以檢查請求方法和操作。

if((request.method == 'POST') && (request.url == '/file-upload') {
  .......
}

更新 -

var http = require("http");
var url = require("url");
var fs = require("fs");

http.createServer(function(req, res) {
  switch (url.parse(req.url).pathname)
  { 
    case '/': 
      console.log('server side methodcalled'); 
        fs.readFile(__dirname + '/index.html',
          function (err, data) {    
            res.writeHead(200);
            res.end(data);
          });
        break;
    case '/file-upload': 
      console.log('server side method called');
      break; 
    default: 
      console.log('server side method called');
      break;
  }
}).listen(3000);

現在您可以連接到本地主機:3000

為了實現文件上傳,您需要閱讀請求的正文並解析出各個不同的部分。 這可能很棘手,所以我強烈建議您使用現有的模塊(例如“強大”),或在幕后調用諸如connect.bodyParser()或express.bodyParser()之類的模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM