从 NodeJS 服务器发送 HTTP 请求

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

我有一个在 NodeJS 服务器上运行的 NextJS 应用程序,它将作为我的三层应用程序的前端。

'use client';

import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
import Select, { SelectChangeEvent }  from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import Box from '@mui/material/Box';
import Grid from '@mui/material/Grid';
import Button from '@mui/material/Button';
import Paper from '@mui/material/Paper';

import { useState } from 'react'

import { storeData } from '../../constants';

export default function ReviewPage(){
    const [ selectedStore, setSelectedStore ] = useState('');

    const handleChange = (event: SelectChangeEvent) => {
        setSelectedStore(event.target.value as string);
    }

    const handleSubmit = async () => {
        const response = fetch("some_backend/path")
    }

    return (
        <>
        {/* FORM */}
        <Grid container spacing={2} justifyContent="center">
            <Grid item xs={6}>
                <FormControl fullWidth>
                    <InputLabel id="demo-simple-select-label">Store</InputLabel>
                    <Select
                        labelId="demo-simple-select-label"
                        id="demo-simple-select"
                        label="store"
                        value={selectedStore}
                        onChange={handleChange}
                    >
                        {/* Map the stores to populate the Select field */}
                        {storeData.map(store => <MenuItem key={store.id} value={store.name}>{store.name}</MenuItem>)}  
                    </Select>
                </FormControl>
            </Grid>
        </Grid>

        <Grid container justifyContent="center">
            <Grid item>
                <Button onClick={handleSubmit} variant="contained">Analyse</Button>
            </Grid>
        </Grid>

        {/* Results */}
        <Grid container>
            <Grid item xs={12}>
                <Paper elevation={3} sx={{padding: "15px"}}>
                    Why do we use it?
                    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
                </Paper>
            </Grid>
            <Grid item xs={12}>
                <Paper elevation={3} sx={{padding: "15px"}}>
                    Why do we use it?
                    It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
                </Paper>
            </Grid>
        </Grid>
        </>
    )
} 

fetch

 调用 
handleChange
 会将请求从浏览器发送到后端。理想情况下,后端将部署在仅限内部的网络上(因此无法从客户端浏览器访问)。如何从 NodeJS 服务器本身而不是从客户端浏览器向后端发送请求。

有什么办法可以做到这一点吗?到目前为止我还没有找到答案。我做错事了吗?还有其他方法可以实现这一点吗?

javascript node.js next.js
1个回答
0
投票
不止一种方法可以做到这一点,但理想情况下您会通过代理来

.pipe

 http 请求和响应。比如:

import http from 'node:http'; async function route(request,response,user){ //if user is x,y,or z const creq = http.request(opts, (cres) => { cres.pipe(response); }); request.pipe(creq); }
    
© www.soinside.com 2019 - 2024. All rights reserved.