How to receive a post or get request with typescript?

Asked

Viewed 739 times

1

i’m starting with nodejs now and would like to use typescript to program compiling for pure javascript. So I’m having trouble finding something succinct so I can understand (since I’m a PHP programmer) how to receive post and get requests. For example, in PHP use like this:

$_POST["foo"] or file_get_contents("php://input") using angular and Angularjs

or

$_GET["foo"]

Can you help me?

  • You’re using angular in your Krint project?

  • Yes. I’m using Angular, but at the moment I’m only developing the back-end

  • Do you want to do a post or get request on the back end or on the front end? Because if it’s on the back, you can use the native Node module. If it is what will be transpilated to the front, then I think in Angular or Axios

  • So, actually, I need to know how to receive information by post or get on typescript, got it? For example, my front-end sends the user’s Cpf via post and I need to know how to receive this Cpf using typescript com in the nodejs environment. It’s become clearer now?

1 answer

1


You can use Node.js and express.js to receive post and get.

Install the express like this: npm install express

declare a variable to receive the express()

var express = require('express');
var app = express();

use the function below to receive the GET request

app.get('/SuaView.extensão', (req, resp) => {

    //Seucódigo
            resp.render("SuaView");
            resp.end();
        });

And right below is the POST request

app.post('/SeuPost', (req, resp) => {

    //SeuCódigo

        resp.render("SuaView");
        resp.end();
});

The Resp.render renders the desired view, if you’re manipulating some information, you can create a variable that takes a function and populates an object. To pass it to the view, you can do it this way: Resp.render("Suaview", { novavariable: objDaFunction });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.