Im trying to add a sitemap at Google Search Console.
I have this code:
Route
router.get("/sitemap", function(req, res){
res.render("sitemap.ejs");
});
Sitemap.ejs
<html>
<head>
<title>Sitemap</title>
<link rel="alternate" type="application/rss+xml" title="" href="sitemap.xml" />
</head>
<body>
</body>
</html>
But when i try to add the sitemap to google it says that the sitemap cant be in html format.
How can i make it valid to Google Search Console?
Solution
var path = require("path");
router.get('/sitemap.xml', function(req, res) {
res.sendFile(path.join(__dirname, 'path', 'sitemap.xml'));
});
Related
I'm trying to use node js to show an image within an html file, but the image doesn't show up.
If I try to open the html file with a browser the image shows up without problem, but when I run it from the node js server it doesn't work.
Here is my node js code:
var http = require('http');
var fs = require('fs');
http.createServer(function (req, res) {
fs.readFile('pruebaFoto.html', function(err, data) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
return res.end();
});
}).listen(8080);
Here is the html code:
<!DOCTYPE html>
<html>
<head>
<meta http-equi="refresh" content="1"> <!--I use this meta tag for refreshing the page each second-->
</head>
<body>
<h1>Image: </h1>
<img src="./image.jpg" alt="image">
</body>
</html>
The image and all the code are in the same path.
You need to create a router so the server can respond to different requests with different content.
There are many options for this, a popular option is to use a library like https://expressjs.com/
I'm very new to node, express etc. I've made a blog-app and i'm facing a problem. I'm using mongoose, node, express and ejs.
When i call
router.get('/posts', function(req, res){ Post.find({}, function(err, posts){
res.render('viewpost.ejs', {
posts
}); }); });
Everything works perfectly fine. I got my posts and css is working as well. The problem is when i call
router.get('/posts/:posts_id', function(req, res){
Post.find({_id: req.params.posts_id}, function(err, posts){
res.render('viewpost.ejs',{
posts
});
});
});
Post seems to be working but in console i got
GET /posts/posts.css 304 1.854 ms
And the viewpost.ejs looks like it's not using the css.
Server file server.js
app.use(express.static('public'))
app.use(express.static(path.join(__dirname, 'public/css/')));
app.use(express.static(path.join(__dirname, 'public/img/')));
viewpost.ejs
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.css"/>
<link rel="stylesheet" href="style.css">
<script src="/bower_components/jquery/dist/jquery.js"></script>
</head>
<body class="cyc">
<% include ./partials/navbar.ejs %>
<%= posts %>
</body>
</html>
So when i use route without req.params everything seems to be working
ok. When i call it with any param my css file is not working.
<link rel="stylesheet" href="style.css"> tries to load style.css from the same path where the HTML page is. So, for /posts, it will try loading /style.css and for /posts/1, it will try to load /posts/style.css. But the latter matches your /posts/:posts_id endpoint, so that gets called instead with posts_id == 'style.css', which is nonsensical.
The solution is quite simply to make the link absolute:
<link rel="stylesheet" href="/style.css">
I was facing the same issue,but finally I solved it.
Do Following:
In Your HTML or EJS File write:
<link rel="stylesheet" href="../style.css">
in head section,it will work for both /post and /post:id
I have a server_render.js as follows. I have used express package of NPM. I have tow webpages in views directory, one is home.ejs and the other is portal.ejs. I used the EJS template engine.
var express=require('express')
var app=express()
const port=process.env.PORT || 10002
//app.use(express.static(__dirname+'/public'))
//template engine
app.set('view engine','ejs');
//homepage
app.get('/',function(req,res){
res.render('home.ejs');
})
//signin page: 127.0.0.1:10002/signin
app.get('/signin',function(req,res){
res.render('portal.ejs');
});
//run nodejs loop server
app.listen(port,function(err){
if(err){
throw err
}
console.log('server is listening on '+port+' ...')
})
views/home.ejs looks like this:
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Portal</title>
</head>
<body>
<div>You need to <a href='portal.ejs'>sign-in</a></div>
</body>
</html>
and views/portal.ejs is this:
<!DOCTYPE html>
<html lang='en'>
<head>
<title>Portal</title>
</head>
<body>
<input name="username" id="username" type="text" placeholder="username"></input>
<input name="password" id="password" type="text" placeholder="password"></input>
</body>
</html>
I want to create a link from home.ejs to portal.ejs. I tried href="portal.ejs" however it did'nt work. I wonder what's the method.
You can't link to the actual ejs files. In your code you have declared the following paths:
//homepage
app.get('/',function(req,res){
res.render('home.ejs');
})
//signin page: 127.0.0.1:10002/signin
app.get('/signin',function(req,res){
res.render('portal.ejs');
});
This means that your express app has urls at '/' and '/signin'. On each of those two paths you have declared to render either home.ejs or portal.ejs as the output HTML.
Just point the links to '/' and '/signin' instead of the actual template files.
I'm making a chat application using a node server but when I start the node server and point my browser to localhost:3000 it loads the HTML document but not the CSS file which is supposed to load when the HTML document loads. How do you load the CSS file so that the HTML document uses it?
HTML document code
<html>
<head>
<title>Socket.IO Chat by Flow</title>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script src="chat.js"></script>
</head>
<link rel="stylesheet" type="text/css" href="css.css">
<body>
<table align="center" id="chat-box">
<td>
<ul id="messages"></ul>
<form action="">
<input type="text" placeholder="Press enter to send a message..." autocomplete="off" id="m">
</form>
</td>
</table>
</body>
JavaScript document code
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = 3000;
app.get('/', function(req, res){
res.sendFile(__dirname + '/chat.html');
});
io.on('connection', function(socket){
});
http.listen(port, function(){
console.log('Listening on port: ' + port);
});
You haven't defined any routes that would provide the CSS file. The only route you've defined is
app.get('/', function(req, res){
res.sendFile(__dirname + '/chat.html');
});
...which will only ever serve chat.html.
To serve other files/resources, define other routes, perhaps a catch-all route that serves any matching file from a public directory.
For instance, this tutorial says you can serve any static files from the directory public by doing this:
app.use(express.static('public'));
More about routing in the routing tutorial. That whole series of tutorials may well be useful, in fact.
I am attempting to capture the information that is being sent from a form in Express and Node.js. Here are the relevant contents of my app.js and index.hjs:
app.js
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
})
index.hjs
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1>{{ title }}</h1>
<p>Welcome to {{ title }}</p>
<form method="post" action="/">
<input type="test" name="field1">
<input type="test" name="field2">
<input type="submit">
</form>
</body>
</html>
When attempting to submit the form on http://expressserver.domain:3000, I receive a 404 error. Does anyone have any ideas on what the issue is and point me in the right direction?
Instead of using app.use, have a real POST route.
app.post("/", function(req, res) {
console.log(req.body);
res.json(req.body);
});
You need an actual route for it to kick in, and you want .post, not .use, because the latter is for all possible HTTP verbs, which means it'll try to access req.body for things that never have one (GET, OPTIONS, HEAD, etc) and crash your script.
I've found a really good example here: http://runnable.com/U0sU598vXio2uD-1/example-reading-form-input-with-express-4-0-and-body-parser-for-node-js which works.