我正在使用reactjs和nodejs编写一个leetcode克隆。为了挑战自己,我将其作为一个类来编写,而不是作为一个整体功能。在任何一种情况下,我似乎都不知道如何让 axios.post("http://localhost:80/python", {code})}>[Run] 返回字段的输入而不是变量。我附上了以下代码: import React from "react"; import CodeMirror from '@uiw/react-codemirror'; import 'codemirror/keymap/sublime'; import 'codemirror/theme/monokai.css'; import axios from 'axios'; import './App.css'; class App extends React.Component { reload = () => { this.setState({ index: this.state.index+1, //index: this.state.index-1 }); } runCode = () => { this.setState({codea: true}) } constructor(props) { super(props); this.state = { DataisLoaded: false, count: 0, index: 0, firstName: '', lastName: '', age: '', email: '', description: '', hobbies: '', location: '', codea: false, code: 'console.log("Hello World")', }; //this.Select = this.Select.bind(this); } componentDidMount() { fetch( //"https://jsonplaceholder.typicode.com/users") "http://localhost") .then((res) => res.json()) .then((json) => { this.setState({ items: json, DataisLoaded: true }); }) } render() { const videos = new Array("https://www.youtube.com/embed/aXXZwyeTJ98"); videos.push("https://www.youtube.com/embed/GnodscC2p-A"); var map = new Map(); for(var s = 0; s < videos.length; s++){ map.set(videos[s]); } let i = 0; while (i < videos.length) { if (i === this.state.count){ var video = videos[i]; } i++; } const { DataisLoaded, items } = this.state; if (!DataisLoaded) return <div> <h1> Please wait some time.... </h1> </div> ; var codea = 0; var code = 'console.log("Hello World")'; return ( <> <div className = "App"> <header className="App-header"> <h1 className="text-1xl font-bold underline"> </h1> <table> <tr><td> <button onClick={() => this.setState({ count: this.state.count - 1 })}> [Last] </button> </td><td><p><code>-</code> Video {this.state.count} <code>-</code> </p> </td><td> <button onClick={() => this.setState({ count: this.state.count + 1 })}> [Next] </button> </td></tr> </table> <iframe key={this.state.index} width="560" height="315" src={video} title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> <div ClassName ="absolute top-20 bottom-40 left-10 right-10 text-11"> </div> <div> <CodeMirror value={this.state.code} options={{ mode: 'python', lineNumbers: true, }} onChange={(editor, data, value) => { this.setState({ codea: false, code: value, } ) }} /> <button onClick={() => axios.post("http://localhost:80/python", {code})}>[Run]</button> <div className="Output"> <pre>{this.state.codea && this.state.code}</pre> </div> </div> </header> </div> </> ); } } export default App; const http = require('http'); const express = require('express'); const cors = require("cors"); const app = express(); const port = 80; const fs = require("fs"); app.use(cors()); app.use(express.json()); const users = require("./users"); app.get('/', (req, res) => { res.send(users) }) app.post('/python', (req, res) => { if (req.body.code != null) { fs.stat('test.py', function (err, stats) { console.log(stats); if (err) { return console.error(err); fs.writeFileSync("test.py", req.body.code); } fs.unlink('test.py',function(err){ if(err) return console.log(err); console.log('file deleted successfully'); fs.writeFileSync("test.py", req.body.code); }); }); var user = req.body.code; console.log({user}); } else { console.log("Error1"); } if (req.body != null) { console.log("Success1"); } else { console.log("Error1"); } if (req.method === 'POST') { console.log("Post1"); } else { console.log("?1"); } }) app.post('/users', (req, res) => { res.json({ message: "success" }); if (req.body != null) { console.log("Success2"); } else { console.log("Error2"); } if (req.method === 'POST') { console.log("Post2"); } else { console.log("?2"); } }); app.listen(port, () => { console.log(`Example app listening on port ${port}`) })` 我已经尝试了所有我能想到的事情。到目前为止还没有任何效果。
import React from "react"; import CodeMirror from '@uiw/react-codemirror'; import 'codemirror/keymap/sublime'; import 'codemirror/theme/monokai.css'; import axios from 'axios'; import './App.css'; class App extends React.Component { reload = () => { this.setState({ index: this.state.index+1, //index: this.state.index-1 }); } runCode = () => { this.setState({codea: true}) } constructor(props) { super(props); this.state = { DataisLoaded: false, count: 0, index: 0, firstName: '', lastName: '', age: '', email: '', description: '', hobbies: '', location: '', codea: false, code: 'console.log("Hello World")', }; //this.Select = this.Select.bind(this); } componentDidMount() { fetch( //"https://jsonplaceholder.typicode.com/users") "http://localhost") .then((res) => res.json()) .then((json) => { this.setState({ items: json, DataisLoaded: true }); }) } render() { const videos = new Array("https://www.youtube.com/embed/aXXZwyeTJ98"); videos.push("https://www.youtube.com/embed/GnodscC2p-A"); var map = new Map(); for(var s = 0; s < videos.length; s++){ map.set(videos[s]); } let i = 0; while (i < videos.length) { if (i === this.state.count){ var video = videos[i]; } i++; } const { DataisLoaded, items } = this.state; if (!DataisLoaded) return <div> <h1> Please wait some time.... </h1> </div> ; var codea = 0; var code = 'console.log("Hello World")'; return ( <> <div className = "App"> <header className="App-header"> <h1 className="text-1xl font-bold underline"> </h1> <table> <tr><td> <button onClick={() => this.setState({ count: this.state.count - 1 })}> [Last] </button> </td><td><p><code>-</code> Video {this.state.count} <code>-</code> </p> </td><td> <button onClick={() => this.setState({ count: this.state.count + 1 })}> [Next] </button> </td></tr> </table> <iframe key={this.state.index} width="560" height="315" src={video} title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe> <div ClassName ="absolute top-20 bottom-40 left-10 right-10 text-11"> </div> <div> <CodeMirror value={this.state.code} options={{ mode: 'python', lineNumbers: true, }} onChange={(editor, data, value) => { this.setState({ codea: false, code: value, } ) }} /> <button onClick={() => axios.post("http://localhost:80/python", {code})}>[Run]</button> <div className="Output"> <pre>{this.state.codea && this.state.code}</pre> </div> </div> </header> </div> </> ); } } export default App;
const http = require('http'); const express = require('express'); const cors = require("cors"); const app = express(); const port = 80; const fs = require("fs"); app.use(cors()); app.use(express.json()); const users = require("./users"); app.get('/', (req, res) => { res.send(users) }) app.post('/python', (req, res) => { if (req.body.code != null) { fs.stat('test.py', function (err, stats) { console.log(stats); if (err) { return console.error(err); fs.writeFileSync("test.py", req.body.code); } fs.unlink('test.py',function(err){ if(err) return console.log(err); console.log('file deleted successfully'); fs.writeFileSync("test.py", req.body.code); }); }); var user = req.body.code; console.log({user}); } else { console.log("Error1"); } if (req.body != null) { console.log("Success1"); } else { console.log("Error1"); } if (req.method === 'POST') { console.log("Post1"); } else { console.log("?1"); } }) app.post('/users', (req, res) => { res.json({ message: "success" }); if (req.body != null) { console.log("Success2"); } else { console.log("Error2"); } if (req.method === 'POST') { console.log("Post2"); } else { console.log("?2"); } }); app.listen(port, () => { console.log(`Example app listening on port ${port}`) })`
我已经尝试了所有我能想到的事情。到目前为止还没有任何效果。