如何将XML文件从PUG发送到节点JS

发布于 2025-02-12 19:06:52 字数 825 浏览 1 评论 0原文

您好,我正在构建一个应用程序,其中您可以上传XML文件并将其发送到API。我正在使用express和nodejs。我是新手。我在帕格(Pug)上创建了上传和发送按钮,并在节点上接收一个函数。前端和后端在同一端口上运行。如何将XML文件发送到后端以阅读并将其发送到API?

这是我的代码:

pug:

    doctype html
html(lang='en')
 head
   title Transport
 body
   h3 Upload XML:
   form(method='post', enctype='multipart/form-data' action='/upload')
    input(type='file', name='xml')
    br
    input(type='submit', name='xml', value='Upload XML')

app.js file:

const express = require('express')
const app = express()
app.set('view engine', 'pug')
app.set('views', './src/views')
app.get('/', (req,res) => {
res.render('index')
});

app.get('/upload', (req,res) => {
    
})

app.listen(3000, () => console.log("Listening on port 3000"))

这是我使用节点和哈巴狗构建的第一个应用程序,这就是为什么我问这个简单的问题。

Hello I am building an app in which you upload an xml file and send it to an api. I am using Pug and NodeJS with Express. I am new to this. I created in pug the upload and send button and a function to receive on node the file. The frontend and backend are running on the same port. How can I send the xml file to the backend to read it and send it to the api ?

This is my code:

Pug:

    doctype html
html(lang='en')
 head
   title Transport
 body
   h3 Upload XML:
   form(method='post', enctype='multipart/form-data' action='/upload')
    input(type='file', name='xml')
    br
    input(type='submit', name='xml', value='Upload XML')

app.js file:

const express = require('express')
const app = express()
app.set('view engine', 'pug')
app.set('views', './src/views')
app.get('/', (req,res) => {
res.render('index')
});

app.get('/upload', (req,res) => {
    
})

app.listen(3000, () => console.log("Listening on port 3000"))

It is my first app built with node and pug and this is why I am asking this easy question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2025-02-19 19:06:52

更改get 发布,然后您的文件将在req.files属性中

change GET to POST and then your files will be in req.files property

栖迟 2025-02-19 19:06:52

您的方法是不匹配的。 '/upload',(req,res)=> {})方法

your method is mismatch.you should change your app.get('/upload', (req,res) => {}) method in app.js file to app.post('/upload', (req,res) => { }) method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文