如何从我的 js 文件发送一个 post 请求到我的路由文件以显示到我的 hbs 文件?

问题描述 投票:0回答:0

我想发送一个帖子从 mysql 获取数据并将其显示到我的东西,但它没有发送任何帖子请求。

这是我的文件的样子:

this is the place were i am sending the post request

podItems.forEach(item => {
    item.addEventListener('click', () => {
        i
        alert("testing alert");
        //podNum = item.id;
        var test ="dog";
        //email: userMail , rows: gridNum, pod:podNum
        $.post('http://localhost:3000/farm',
        { testsend: test},
        function(res){
            alert(res.testchange);
        });
    });
});

这是我想将其发送到修改:pages.js

const express = require('express');
const authController = require('../controllers/auth');
const router = express.Router();

router.get("/", (req, res) => {
    res.render("index.hbs");
});

router.get("/produce", (req, res) => {
    res.render("produce.hbs");
});

router.get("/contact", (req,res) => {
    res.render("contact.hbs");
});

router.get("/signup", (req, res) => {
    res.render("signup.hbs");
});

router.get("/login", (req, res) => {
    res.render("login.hbs");
});

router.get("/forgot", (req, res) => {
    res.render("forget.hbs");
});

router.get("/change", (req, res) => {
    res.render("change.hbs");
});

router.get("/farm", authController.isLoggedIn, (req, res) => {
  //console.log(req.user);
    if( req.user ) {
      res.render('farm.hbs', {
        user: req.user
      });
    } else {
      res.redirect('/login');
    }
});

router.post("/farm"), async(req, res) => {
  console.log(req.body)
  
var gottest = "gotten";
res.send({
  testchange : gottest

});
}

router.get("/export", (req, res) => {
  res.render("export_page.hbs");
});


router.get('/profile', authController.isLoggedIn, (req, res) => {
    //console.log(req.user);
    if( req.user ) {
      res.render('profile.hbs', {
        user: req.user
      });
    } else {
      res.redirect('/login');
    }
    
  });

module.exports = router;

可能相关的一些文件:app.js

const express = require("express");
const path = require('path');
const mysql = require("mysql");
const dotenv = require('dotenv');
const cookieParser = require('cookie-parser');

dotenv.config({ path: './.env'});

const app = express();

const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'nodejs-login'
});

const plantdb = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'plantdb'
})


const publicDirectory = path.join(__dirname, './public')
app.use(express.static(publicDirectory));

app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());

app.set('view engine', 'hbs');

db.connect( (error) =>{
    if(error) {
        console.log(error)
    }
    else{
        console.log("MySQL is in")
    }
})

plantdb.connect( (error) =>{
    if(error) {
        console.log(error)
    }
    else{
        console.log("MySQL is in")
    }
})

app.use('/',require('./routes/pages'));

app.use('/auth', require('./routes/auth'));

app.listen(3001, () => {
    console.log("Server started on Port 3001");
})

我的主要目标是让“用户”输入几个按钮,然后页面将显示来自 mysql 服务器的信息到页面上,供他们查看/编辑。我只是卡住了 rn 因为帖子没有发送任何东西。我已经放置了一些控制台日志,所以看看它是否甚至到达了帖子,但它只到达了说“测试警报”的警报。为什么不执行发布请求?或者有没有更好的方法来为我的程序从 mysql 发送和接收数据?

node.js post hbs
© www.soinside.com 2019 - 2024. All rights reserved.