javascript - How to handle a get request with node.js (express) -



javascript - How to handle a get request with node.js (express) -

i'm node.js newbie , i'm creating first big app (i'm using express). need have webpage perform javascript canvas-drawing when user loads id request, e.g.

www.mywebsite.com/page?id=22

i know can handle simple

var express = require("express"); var app = express(); app.get('handle',function(request,response){ //request.id });

but don't know how start webpage asked drawing id. every tutorial on net on express , explains how handle requests... question "what happens next?"

rephrased: i'm not sure how should tell html page "you need draw associated id" express , send page user.

you can take id params , after homecoming response based on id.

var express = require('express'); var app = express(); app.get("/page/:id",function(request, response){ var id = request.params.id; // id // send response user based on id var obj = { id : id, content : "content " +id }; response.writehead(200, {"content-type": "application/json"}); response.write(json.stringify(obj)); });

notes:

you utilize /page/:id create urls www.mywebsite.com/page/22 or www.mywebsite.com/page?id=22 , can have acces id on server request.params.id (output: 22).

with response can write response server. in illustration returned json object.

in writehead 200 come status means ok , , content-type means homecoming json object

you can homecoming want, page or else, illustration (poc).

javascript node.js express get

Comments

Popular posts from this blog

Using Android Volley to post an array to PHP -

python - How to add an model instance to a django queryset? -

angularjs - No 'Access-Control-Allow-Origin' error from local domain to public API -