node.js - How to pass data to client out of "app.get"? -
how pass data client out of "app.get"?
there 2 server(front, back)
clients connect front server,
front server server, server db
when client logs in front server
front server pass request server
back server search log in information db
after search, server return result front server
however, because front server http server
app.get ("/ login /: id / password), function (request, response) { // send login data server // because dont know db result can't response 'response' variable };
when front server received result of log in
the code out of "app.get"
so can't access response variable ( response.end( "result" ) )
how pass result of log in client?
you can pass response client side of response
object.
app.get("/ login /: id / password), function (request, response) { // send login data client side var logindata = { name: 'admin', password: 'admin' } console.log(logindata); response.writehead(200, { 'content-type': 'text/json' }); response.write(logindata); response.end(); };
edited post: post request backend server
for that, need add request
module of node.js
package.json
.
{ "dependencies": { "request": "*" } }
now, update terminal using npm update
command. request
module added node_modules
folder.
its time call backend server.
app.get("/ login /: id / password), function (request, response) { // import request module var request = require("request"); request({ uri: '', // pass url method: "post", // get/post form: request.body // contains data (login/password) sent client side }, function(error, resp, body) { console.log(body); response.writehead(200, { 'content-type': 'text/json' }); response.write(body); response.end(); }); };
Comments
Post a Comment