Whenever I save app.js files in react it errors always comes up and unexpected token

Cr3 D
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.

Scotty Jamison

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.

edited at
0

Comments

0 comments
Login to comment

Related

Jest with create-react-app - unexpected token errors

React comes back with an unexpected token error when I do a dynamic destructuring of my object

Vite React App - Unexpected Token Index.js

Unexpected token i in JSON at position 6 - react redux app

Parsing error: unexpected token, expected "," and my react app is not showing up in browser. HELP PLEASE

Irrespective of what application owns active window, whenever I hit F1, Firefox dialog comes up

Why do I get this error: Invalid JavaScript! You cannot save until you fix all errors: Unexpected token )

React app: Jest encountered an unexpected token

Save token that comes in response to Api

React Native. How to get user local authentication whenever the app comes from the background?

Unexpected token errors in Powershell

componentDidMount unexpected token error [React.js]

React JS Jest causing "SyntaxError: Unexpected token ."

React JS : Parsing error: Unexpected token, expected ";"

I want that whenever I press SPACE, a new pygame window will pop up. But the program is showing some errors whenever i press SPACE

Uncaught SyntaxError: Unexpected token '<' (at index.js:1:1) REACT APP

Vue.js unexpected token in browser but npm run build with no errors

How can I fix NPM which always says unexpected token =

why am I getting unexpected token error in react project in header.js?

React JS App freezes the browser whenever typing something

i am not able to create a new react app i get these errors below i belive i have everythig up to date

React JS Azure DevOps Web App Wont Run But Files Are Present in Server and No Errors in Pipelines

Unexpected Token - Jest for existing React + Web-pack app

SyntaxError: Unexpected token < in JSON at position 0 - React app in Rails API

'Unexpected token import' while testing in create-react-app

Create-React-App build - "Uncaught SyntaxError: Unexpected token <"

Firebase deployed react app not working. Uncaught SyntaxError: Unexpected token '<'

Heroku create-react-app deploy Uncaught SyntaxError: Unexpected token <

React App works locally but getting Unexpected Token error in CodePen

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive