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
- Host: GitHub
- URL: https://github.com/suryakarmakar/nodejs-response
- Owner: SuryaKarmakar
- License: mit
- Created: 2021-12-08T16:31:14.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-12-11T10:24:11.000Z (over 3 years ago)
- Last Synced: 2025-01-14T13:16:19.305Z (4 months ago)
- Topics: http-response, response, vanilla-nodejs
- Language: JavaScript
- Homepage:
- Size: 13.7 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();
```