How to use ReactJS and Babel with JSON API in Golang - node.js

I have a simple REST API written in Golang. Now I want to have an async UI and wanted to go with ReactJS + Babel or Typescript + fetch() to get data from my JSON api. Don't want nodejs or anything else.
Most of the examples require nodejs and I can't find something to have as an example to build the UI. Could someone help with an example?

Figured it out finally.
Basically using babel-standalone helped a lot. And using axios for fetching data was easy enough.

Related

How to LInk Backend(Node.js) to front-End(React)

I am starting out on React.js after learning Node.js using Express. But I don't know How to Link my Backend App to front end React files? Can someone provide me a simple solution to the problem
You can begin with this steps, you got a thousands of topics about to make a complete and solid infrastructure:
Initialise your node.js server with port, cors (if you need)
Learn how express work with node.js and create your first route
Initialize your react project with or without npx create-react-app
Create your first frontend component and fetch data from your API
Link : https://www.freecodecamp.org/news/create-a-react-frontend-a-node-express-backend-and-connect-them-together-c5798926047c/

A library for nodejs connection with backend

i am creating a node application which will use a another node backend, like a CLI program, which Node.js library or framework will i use for this situation? the axios not is working in my tests.
thanks for reading.
If you want to do http requests from within node.js there are several options: https://www.npmjs.com/package/node-fetch, axios is also available. Could you please share some errors you get?
I always use node-fetch because I am most familiar with the fetch API, previously I used this package: https://www.npmjs.com/package/xmlhttprequest
As seen in your answer you are using /users as url, please use the full url. Also catch any unexpected errors.
So axios.get('http://domain/users')
I SOLVED THE PROBLEM!
the problem was in the axios connection, now its:
async function execQuery(apps){
data = ({
name: "ederson",
apps: "neofetch"
})
const response = await axios.put('http://localhost:3333/users', data
).then(console.log(apps))
}
Thanks for help me, Laurent Dhont and Ahmed Hammad!

How can I test (integration-testing) with supertest a Node.js server with Passport JS using facebook/google... strategies with OAUTH2?

I have a Node Js application and I'm integration-testing my app with supertest/superagent + nockjs.
I have a problem, because I want to test my login rest apis using supertest to REPLY with a FAKE PROFILE RESPONSE + token for example for facebook/google/github and so on. (I'm not interested in LocalStrategy, because it' very simple)
How can I do that?
I'm trying with GitHub, and I wrote this code (not working) absolutely wrong, probably very stupid without any sense...It was an experiment XD.
nock('https://github.com/login/oauth')
.get('/authorize?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fauth%2Fgithub%2Fcallback&scope=user%3Aemail&client_id=XXXXXXXXXXXXXXXXXXXX')
.reply(302,undefined,
{
location : "http://localhost:3000/api/auth/github/callback?code=ab7f9823f03071209b26"
}
)
.get('http://localhost:3000/api/auth/github/callback?code=ab7f9823f03071209b26')
.reply(200, responseMocked);
PS: probably I made a mistake with url and status, I don't know.
Also, where I should set the connection.sid's cookie ?
How can I fix/rewrite this code to be able to integration-testing my application?
I'm also interested to use passportjs stub/mock, but I want a library supported and well documented.
UPDATE: I fixed the name of the mocked profile object (responseMocked)
Thank you,
Stefano.

Node.js works with CouchDB and Backbone.js, How json is being served?

I am trying to build a test app for learning Node.js. I came from wordpress background and Apache has setup most of backend logics for me. But now, I have to build my own. I have a question about how to serve JSON files from server side to client side. What is the workflow -- Backbone.js handle all client side Data manipulation, send/save/get/fetch from couchDB, serve JSON object from NODE.js backend?
I am using Express Microframework for building HTTP server, installed the Cradle middleware for access CouchDB NoSQL database. I successfully posted the data from Client side HTML (Jade template engine) to the CouchDB Database/Document and able to retrieve those data back from Server through Cradle middleware. Things work out great. But it was all done by Backend.
I want to use Backbone.js for my client side Javascript. Backbone.js is looking for JSON object which send back from the HTTP server. Cradle Middleware is able to create JSON object but only send them directly to the Jade Template, I could use Jade syntax for loop to iterate over the data object but it still not meet what I want for Backbone.js handle all the data entry. I realize that I need to get JSON obj via ajax ( either a file generated by HTTP then send back to the client OR send straight object to the client ). Cradle Middleware could not do that.
I did more research on this questions. I tried CouchApp, it does what I need. Using the Backbone.js to handling all the data, send/save/fetch data from CouchDB database. But it is a running in CouchApp, it is not an Express Node.js workflow. ( Maybe I am wrong or just do not how it work )
I tried backbone-couchdb.js. I read through the Details and still do not know it is going to help me to get what I want. ( Maybe need more tutorial or code example ). I am still thinking that I need a Backbone CouchDB driver to connect those two and somehow serving them by NODE.js backend.
Is there anybody who could tell me about how JSON file is being served by Node.js, how backbone.js interact with data save/fetch/get from CouchDB? What is the best practice / workflow? Other good resources, code examples, useful tools?
Cradle Middleware is able to create JSON object but only send them directly to the Jade Template
This is incorrect. You can just send the json back without rendering a template.
function(req, res, next){
db.view('user/byUsername', { key: 'luke' }, function (err, doc) {
res.send(doc); // or res.json(doc);
});
}

Developing Json services with node

Does it make sense to develop a rest service with node.js backed with mongodb? Is there a framework to make this easy like express?
Thanks.
Why can't you use express? It implements all CRUD methods through:
app.get(...);
app.post(...);
app.put(...);
app.del(...);
In these function calls you can handle your mongodb queries and send JSON objects back to the client, if appropriate.
I hope I could help! :)
Like graydsl said express allready supports these verbs. To parse JSON you would just use JSON.parse and to stringify you would use JSON.stringify. I would use Mongoose to talk to mongodb. Also to help you write clean code I would practice TDD/BDD using mocha. Finally I think you should have a look at underscore, async and superagent.

Resources