Reputation: 93
I simply make a little templating system with EJS, Express.js on node.js but the render is a bit wtf...
And here the code of my server.js :
var fs = require('fs');
var http = require('http');
var https = require('https');
//var privateKey = fs.readFileSync('sslcert/server.key', 'utf8');
//var certificate = fs.readFileSync('sslcert/server.crt', 'utf8');
//var credentials = {key: privateKey, cert: certificate};
var express = require('express');
var app = express();
app.set('view engine', 'ejs');
//Here the config of page
app.get('/', function(req, res){
//Accueil -> redirection vers un chat aléatoire
res.setHeader('Content-Type', 'text/plain');
res.status(500).send('Test !');
});
app.get('/about', function(req, res){
res.setHeader('Content-Type', 'text/plain');
res.status(500).send('Test !');
});
app.get('/auth/login', function(req,res){
res.setHeader('Content-Type', 'text/plain');
res.status(500).send('Test !');
});
app.get('/auth/register', function(req,res){
res.setHeader('Content-Type', 'text/plain');
res.status(500).send('Test !');
});
app.get('/chat', function(req,res){
res.setHeader('Content-Type', 'text/plain');
res.render('redirect.ejs', {});
})
app.get('/chat/:chatnum', function(req,res){
if(parseInt(req.params.chatnum, 10) < 101){
res.setHeader('Content-Type', 'text/plain');
res.status(500).send('Test !' + req.params.chatnum);
}
else {
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !!');
}
});
app.use(function(req, res, next){
res.setHeader('Content-Type', 'text/plain');
res.status(404).send('Page introuvable !');
});
var httpServer = http.createServer(app);
//var httpsServer = https.createServer(credentials, app);
httpServer.listen(8080);
//httpsServer.listen(8443);
(i've disabled https currently)
So if someone know why it is not rendering html as html but as text...
Thanks in advance...
EDIT : P.S. you can see the template code in the browser as it is in the file redirecting.ejs EDIT 2 : adding code format for redirect.ejs
<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
</head>
<body>
<h1 style="text-align: center">Redirecting...</h1>
<script>window.location.href = "/ + " (Math.random().trunc() * 100) + 1</script>
</body>
</html>
Upvotes: 0
Views: 2682
Reputation: 2173
the header content-type should not be "text/plain". you can remove the line res.setHeader('Content-Type', 'text/plain');
for all route who need to send html.
Be aware that response are cached, and if the content do not change but the header does, you will not see the change.
You can use ctrl+F5
to force reload your page.
Upvotes: 3
Reputation: 2758
Remove the res.setHeader('Content-Type', 'text/plain');
line from any response that should be returning HTML.
That way the browser will know to render it instead of displaying the raw text.
Upvotes: 2