Is it possible in Node.js to return HTML (e.g. <div>Test</div>) as response to client?
I saw that option in Express on sendFile() method().
Is there something similar in Node?
I found this solution:
var myHtmlData;
fs.readFile('./index.html', (err, data) => {
if(err){
throw err;
} else {
myHtmlData = data
}
})
// and after use it
response.write(myHtmlData)
But I was wondering is it possible to do it with some method similar to sendFile() to write html directly like this <div>Test</div> without reading it from another file.
Sure. It's pretty simple. The following code returns the response as HTML to the client.
var http = require('http');
http.createServer(function(req, res){
if(req.url === "/"){
res.writeHead(200, { 'Content-Type':'text/html'});
res.end("<div><p>Test<p></div>");
}
}).listen(3000);
And in case you want to serve an HTML or JSON file as a response, you can execute the following code.
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res){
if(req.url === '/I_want_json'){
res.writeHead(200, { 'Content-Type':'application/json'});
var obj = {
firstName: "Jane",
lastName: "Doe",
};
res.end(JSON.stringify(obj));
}
else if (req.url === '/I_want_html'){
res.writeHead(200, { 'Content-Type':'text/html'});
html = fs.readFileSync('./index.html');
res.end(html);
}
else{
res.writeHead(404);
res.end();
}
}).listen(3000, '127.0.0.1');
Do not forget to set the Content-Type as mentioned since it is a mandatory part for client to distinguish the type of response.
First, you need to mention the content type as HTML. Then read the HTML file using fs module and send its data as a response. Don't forget to end the response using end() method.
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
res.setHeader("Content-Type", "text/html");
fs.readFile('./views/index.html', (err, data) => {
if(err) {
console.log(err);
res.end();
} else {
res.write(data);
res.end();
}
})
});
Related
I just set up my first node HTTP server, and I am trying to get the response data from a JSON file in my application. When I declare a JSON object in the server.js file, all works well.
data = "{"sample json" : "this is a test"}";
But I want to replace data with a static JSON file at data/sample.json
Here's an example in my server.js file
const http = require("http");
const hostname = "localhost";
const port = 3000;
const server = http.createServer(function(req, res) {
data = // this is where I want to get the JSON data from data/sample.json
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(data);
res.end();
});
Solved with fs.readFile()
const http = require("http");
const hostname = "localhost";
const port = 3000;
const fs = require('fs');
const server = http.createServer(function(req, res) {
filePath = './data/sample.json';
if (req.path == '/data/sample.json') {
fs.readFile(filePath, function(error, content) {
if (error) {
if (error.code == 'ENOENT') {
response.writeHead(404);
response.end(error.code);
}
else {
response.writeHead(500);
response.end(error.code);
}
}
else {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(content);
}
});
}
else {
response.writeHead(404);
response.end('restricted path');
}
});
In case someone else tumbles upon this question. The answer above has ton of typos and errors and incomplete. Here is a working solution.
const http = require("http");
const hostname = "localhost";
const fs = require('fs');
const port = 8080;
const server = http.createServer(function(req, res) {
filePath = './data/sample.json';
if (req.url == '/api/data') {
fs.readFile(filePath, function(error, content) {
if (error) {
if (error.code == 'ENOENT') {
res.writeHead(404);
res.end(error.code);
}
else {
res.writeHead(500);
res.end(error.code);
}
}
else {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(content);
}
});
}
else {
res.writeHead(404);
res.end('404 NOT FOUND');
}
});
server.listen(port, hostname, () => {
console.log('Server started on port ', port);
});
I'm new programmer to node.js. I trying to create vanilla server in node.js. In my client, I used ES6 modules. when I start my server and search for http://localhost:3000/ in my browser, HTML and CSS loaded but for javascript have this error:
I have four javascript modules for client side and in HTML I use this code for load javascript moduls:
<script type="module" src="js/controller.js" async></script>
My server code :
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 3000;
const server = http.createServer();
server.on('request', (req, res) => {
let routes = {
'GET': {
'/': indexHtml,
}
}
console.log(req.url)
let handler = routes[req.method][req.url];
handler = handler || readFile;
handler(req, res);
})
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`)
});
function indexHtml(req, res) {
readFile({ url: '/index.html' }, res);
}
function readFile(req, res) {
var filePath = path.join(__dirname, '../client/', req.url);
// console.log(filePath)
fs.readFile(filePath, (error, data) => {
if (error) {
res.writeHead(404);
res.write('Not found error 404');
res.end()
} else {
res.writeHead(200);
res.write(data);
res.end();
}
})
}
How can I solve this error and serve javascript modules, Thanks a lot for your help.
With comment #derpirscher, I change my reader function with this code :
function readFile(req, res) {
var filePath = path.join(__dirname, '../client/', req.url);
fs.readFile(filePath, (error, data) => {
if (error) {
res.setHeader('Content-Type', 'text/html');
res.writeHead(404);
res.write('Not found error 404');
res.end()
} else {
const url = req.url === '/' ? '/index.html' : req.url;
if (req.url.includes('js')) res.setHeader('Content-Type', 'application/javascript');
if (req.url.includes('css')) res.setHeader('Content-Type', 'text/css');
if (req.url.includes('html')) res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write(data);
res.end();
}
})
}
What I want to do is to make a request using the "request" module when server receives a request, and return the content of that "request" back to the client. Is it possible?
const http = require("http");
const request = require("request");
const URL = "???";
const server = http.createServer();
server.on('request', (req, res) => {
// called once for every HTTP request
out_res = res;
make_request((err, res, body) => {
out_res.writeHead(200, {res});
out_res.write(body);
out_res.end();
});
});
function make_request(callback) {
request(URL, (err, res, body) => {
callback(err, res, body);
});
}
module.exports = () => {
server.listen(8080);
console.log('server start');
};
I got an error: ERR_STREAM_WRITE_AFTER_END, I've been a long time without node.js, but my friend asked me about some code and I just rewrite as above.
Ofcourse you can do that
server.on('request', (req, res) => {
request({uri: URL}).pipe(res);
});
Just pipe the response of API call to your router response object.
Here is how I would advise you to write your server code
var server = http.createServer(function(req,res){
if(req.url === '/' || req.url === '/index'){
request({uri: URL}).pipe(res);
}
.... //other conditions
});
server.listen(3000,'127.0.0.1')
Moreover, you can/should consider using express, it's really cool and easy to use to define routes etc
var express = require("express");
var app = express();
var fs = require("fs");
var path = require("path");
app.use('/node_modules', express.static('node_modules'));
app.get("/", function (req,res) {
pathname = path.join(__dirname,"index.html");
fs.readFile(pathname,function (err,data) {
if(err){
throw err;
}
res.send(data);
// the res.send(data), the webpage receive the data, and download it instead of showing it as HTML webpage.
}
)
});
app.listen("3000",function () {
console.log("express server constructed");
})
I will tell you the approachs to do
fs.readFile(pathname,function (err,data) {
if(err){
throw err;
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
}
If you don't want readFile you can use this.
res.sendfile(pathname);
use
res.sendFile(pathname);
instead of
res.send(data);
I wrote a very simple program to demonstrate request handling in Node (actually following a tutorial), but the server seems to take forever to respond to the GET / request. Here's the code I'm using:
const http = require('http');
const url = require('url');
let routes = {
'GET': {
'/': (req, res) => {
res.writeHead(200, {'Content-type': 'text/html'});
res.end('GET /');
}
},
'POST': {
},
'NA': (req, res) => {
res.writeHead(404);
res.end('Content not found');
}
}
function router(req, res) {
let baseURI = url.parse(req.url, true);
// the function that gets resolved and used to handle the request
let resolveRoute = routes[req.method][baseURI.pathname];
}
http
.createServer(router).listen(3001, () => {
console.log('Listening on port 3001');
});
Something I'm doing wrong?
Found it myself.
I was resolving the handler function but not calling it. Adding resolveRoute(req, res); to the end of the router() function makes it work. :)