Add a comment. Active Oldest Votes. Khauri Khauri 3, 1 1 gold badge 7 7 silver badges 17 17 bronze badges. Check your stack trace and see what it says and if it's locating the file. Remember that the require function starts looking in the directory in which the file it was called in is located, not the root of your application.
Based on the sample you've supplied, your json is fine. This error may be coming from somewhere else in your application. Check the error stack trace for the line number and file that's causing it. It may be coming from improper usage of the fs filesystem module.
Another option is to use sendFile and set the content type header. Ian Ian 7, 2 2 gold badges 43 43 silver badges 51 51 bronze badges.
Try res. Is there a problem with my JSON do you think? Show 1 more comment. Soth Soth 2, 2 2 gold badges 24 24 silver badges 24 24 bronze badges. Read the file first and then send the json to the client. Arpit Solanki Arpit Solanki 8, 2 2 gold badges 36 36 silver badges 57 57 bronze badges.
Thanks so much. I nested that in my app. Is that what I should've done? Here is the code I have: app. Any clue why? You first have to add a require statement. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Introducing Content Health, a new way to keep the knowledge base up-to-date. In express json can be sent from the server to a client with response methods like res.
In late versions of express such as 4. In this post I will be coving some basics and more about expressjs and json when it comes to both sending it and receiving it to and from a client system. To send JSON from a server side script with expressjs just use the json response method, by just calling the method and passing the object that you want sent as JSON as the first argument.
To gain a deeper understating and appreciation for the res. Express has a few methods like this that help make code cleaner, sometimes they might not always be the best choice though.
If for some reason you want to have control over the Content-Type header you would have to do something like this.
0コメント