https://github.com/sury4karmakar/nodejs-response
⚡how to send a http response (html or json) to the frontend using vanilla node js
https://github.com/sury4karmakar/nodejs-response
http-response response vanilla-nodejs
Last synced: 6 months ago
JSON representation
⚡how to send a http response (html or json) to the frontend using vanilla node js
- Host: GitHub
- URL: https://github.com/sury4karmakar/nodejs-response
- Owner: sury4karmakar
- License: mit
- Created: 2021-12-08T16:31:14.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-04-26T08:53:53.000Z (12 months ago)
- Last Synced: 2025-07-05T10:49:05.454Z (9 months ago)
- Topics: http-response, response, vanilla-nodejs
- Language: JavaScript
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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();
```