class App extends Component {
render() {
return (
const jwt = require('jsonwebtoken')
app.use(express.json())
const posts = [{
username: 'Cr3',
title: 'Post 1'
}]
const express = require('express')
const app = express()
app.get('/posts', (req, res) => {
res.json(posts)
})
app.post('/login', (req, res) => {
//Auth the user using tokens
const username = req.body.username
const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET)
res.json({ accessToken: accessToken }) //Create access token
const user = { name: username } //label name as username
})
app.listen(3000)
<
div id = "colorlib-page" >
<
div id = "container-wrap" >
<
Sidebar > < /Sidebar> <
div id = "colorlib-main" >
<
Introduction > < /Introduction> <
About > < /About> <
Functions > < /Functions>
<
/div> < /
div > <
/div>
);
}
}
export default App;
This is the code, I have no idea why whenever It saves it just goes crazy and shows a bunch of errors, I tried to turning off automatic formatting as well as changing app.js to app.jsx which made it worse for a while, many times when I re-run my node.js server the errors seem to keep pointing at the const jwt stating unexpected token.
OK, there's a couple problems here.
Firstly, you have a bunch of stuff in the middle of your return that can't go there.
You'll want your App class to look like this instead:
class App extends Component {
render() {
return (
<
div id = "colorlib-page" >
<
div id = "container-wrap" >
<
Sidebar > < /Sidebar> <
div id = "colorlib-main" >
<
Introduction > < /Introduction> <
About > < /About> <
Functions > < /Functions>
<
/div> < /
div > <
/div>
);
}
}
Secondly, you're mixing front-end and back-end stuff together. React is a front-end library, it runs in the browsers to help render pages. express is a back-end library to serve the client whatever you need. I'm not exactly sure what you were trying to get this code to do, but you actually don't need any fancy express backend to serve react code, it can be served using a file-hosting server like apache. (Certainly there are ways to do server-side rendering, and return front-end data using express, but not like this).
I would recommend putting all of your express stuff in one backend project, and all of your react stuff in a separate front-end project. The front-end can do REST calls to your running backend if it needs to communicate with it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments