【发布时间】:2019-09-03 07:29:03
【问题描述】:
我正在尝试通过node.js 和express 在reactjs 应用程序中打开用户提供的URL。
我正在使用material-ui 和axios。请在下面找到 UI 代码。
实际上这是一个 UX 测试项目的 POC,其中给出了要测试的应用程序的 URL,并且该应用程序将在父(主)应用程序中打开以进行测试。
UrlForm.js
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import axios from 'axios';
const styles = theme => ({
container: {
display: 'flex',
flexWrap: 'wrap',
},
textField: {
marginLeft: theme.spacing.unit,
marginRight: theme.spacing.unit,
},
dense: {
marginTop: 16,
},
menu: {
width: 200,
},
root: {
...theme.mixins.gutters(),
paddingTop: theme.spacing.unit * 2,
paddingBottom: theme.spacing.unit * 2,
},
button: {
margin: theme.spacing.unit,
},
});
class UrlForm extends React.Component {
state = {
url: ''
};
handleChange = name => event => {
this.setState({
[name]: event.target.value,
});
};
sendData = () => {
console.log(this.state.url);
axios.post("http://localhost:3001/openurl", { url: this.state.url })
.then(res => console.log('Data send'))
.catch(err => console.log(err.data))
}
render() {
const { classes } = this.props;
return (
<Paper className={classes.root} elevation={1}>
<form className={classes.container} noValidate autoComplete="off">
<TextField
id="outlined-name"
label="URL"
className={classes.textField}
value={this.state.url}
onChange={this.handleChange('url')}
margin="normal"
variant="outlined"
/>
<Button variant="outlined" color="primary" onClick={() => { this.sendData(); }} size="small" className={classes.button}>
Open
</Button>
</form>
</Paper>
);
}
}
UrlForm.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(UrlForm);
在这里,我正在发送要在 UI 上打开的 url。
对于我使用express-generator 的服务器,下面是路由器的代码。
还使用cors 并设置标题。
app.js
app.use((req, res, next) => {
// Check if the origin is whitelisted in the env vars
res.set({
// standard CORS headers
'Access-Control-Allow-Origin': 'http://localhost:3000',
'Access-Control-Allow-Headers': 'Content-Type, Authorization, Accept, Accept-Language',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, PATCH, DELETE',
// addresses security issues identified by automated pen testing
'X-Frame-Options': 'DENY',
'X-Content-Type-Options': 'nosniff',
'X-XSS-Protection': 1,
});
next();
});
routes/index.js
// routes/index.js
import express from 'express';
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.post('/openurl', function(req, res, next) {
console.log(req);
console.log(res);
})
export default router;
在路由 /openurl 中,我在 req.body 中获取 URL。如何获取给定的 url 响应并将其发送到客户端以在 UI 上打开它?
这是正确的方法吗?因为我正在寻找可能的选择。
【问题讨论】:
-
“打开”是什么意思?你想在哪里打开它?在浏览器中,还是在您的节点服务器上?您想访问服务器上的 html 吗?
-
@Beaulne 添加了截图以供参考。并修改了描述。
-
抱歉,Open 是什么意思?就像显示网站的url去做url的字符串
http://www.theurl.com? -
@Beaulne 是的,在应用程序中显示网站,该网站 url 将由用户提供。
标签: node.js reactjs express axios material-ui