An open API service indexing awesome lists of open source software.

https://github.com/suryakarmakar/nodejs-response

⚡how to send a http response (html or json) to the frontend using vanilla node js
https://github.com/suryakarmakar/nodejs-response

http-response response vanilla-nodejs

Last synced: 3 months ago
JSON representation

⚡how to send a http response (html or json) to the frontend using vanilla node js

Awesome Lists containing this project

README

        

# Sending Response To The Frontend

- **res.writeHead** to set any response headers.

```js
res.writeHead(200, { "Content-Type": "text/html" });
```

- **res.write()** to send the actual content for the response. the content should be either raw html or json data.

```js
res.write("

Welcome to vanilla node server

");
```

or,

```js
res.write(JSON.stringify(data));
```

- **res.end()** to end the response.

```js
res.end();
```

## Send html response :

```js
res.writeHead(200, { "Content-Type": "text/html" });
res.write("

Welcome to vanilla node server

");
res.end();
```

## Send json response :

```js
res.writeHead(200, { "Content-Type": "application/json" });
res.write(JSON.stringify(data));
res.end();
```