发布请求未被调用

时间:2018-09-20 13:24:51

标签: javascript html node.js

所以我正在学习node.js,但遇到了麻烦。我的帖子请求没有被触发。

var http = require('http');
var express = require('express');
var app = express();
var fs = require('fs');

设置服务器

var server = http.createServer(function(req,res){
  console.log('request was made: ' + req.url);
  if (req.url === '/home' || req.url === '/'){
    fs.readFile("./index.html",null,function(error,data){
      if (error) throw error;
      res.write(data);
  res.end();
});
  } else {
      console.log("404");
  }
})

拒绝工作的要求

app.post('/test', function(req,res){
  console.log("post req came thru");
})

听...

server.listen(8080)

HTML

<!DOCTYPE html>
<html>
<head>
  <title>CRUD App</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form action="/test" method="POST" >
  <input type="text" name="test"></input>
  <button>Submit</button>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要将Express html, body { width: 100%; margin: 0; } #welcome-section { box-sizing: border-box; width: 100%; background-size: cover; background-position: center; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; padding: 0 20px; text-align: center; color: white; font-family: "Montserrat", sans-serif; } #bgVideo { width: 100%; position: absolute; top: 0px; left: 0px; min-width: 100%; min-height: 100%; z-index: -1; box-sizing: content-box; } 作为参数传递给app函数,否则它将无法处理您的任何请求。或者,或者使用实际的http.createServer方法:

app.listen