Как перенаправить на другую страницу с помощью NodeJS - PullRequest
0 голосов
/ 07 мая 2020

Я новичок в NodeJS. Я хочу, чтобы страница перенаправлялась на другую страницу при нажатии на первой странице. Может ли кто-нибудь помочь мне решить проблему. Любая помощь будет оценена. Вот код

app. js

var express = require('express');
var app = express();

app.get('/', function(req,res){
  res.status(200).sendFile(__dirname + '/index.html');
});

app.get('/newPage.html', (req, res) => res.redirect('/newPage.html'))

var server = app.listen(process.env.PORT || '3000', function(){
  console.log('App listening on Port %s', server.address().port);
  console.log('Press Ctrl+C to quit');
});

index. html

<!DOCTYPE html>
<html>
<head>
    <title>NodeJS - HTML/CSS</title>
</head>
<body>
    <h1>NodeJS HTML and CSS Demo</h1>
    <p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Maiores saepe quae debitis alias illum, animi sunt iste
    </p>

        <a href= "newPage.html">new page</a>

</body>
</html>

newPage. html

<!DOCTYPE html>
<html>

<head>
    <title>New page</title>
</head>

<body>
    <h1>This is the new page</h1>
</body>

</html>

1 Ответ

0 голосов
/ 07 мая 2020

Вы можете сделать следующее:

приложение. js

var express = require('express');
var app = express();

app.get('/', function(req,res){
  res.status(200).sendFile(__dirname + '/index.html');
});

app.get('/newPage', function(req,res){
  res.status(200).sendFile(__dirname + '/newPage.html');
});

var server = app.listen(process.env.PORT || '3000', function(){
  console.log('App listening on Port %s', server.address().port);
  console.log('Press Ctrl+C to quit');
});

индекс. html

<!DOCTYPE html>
<html>
<head>
    <title>NodeJS - HTML/CSS</title>
</head>
<body>
    <h1>NodeJS HTML and CSS Demo</h1>
    <p>
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Maiores saepe quae debitis alias illum, animi sunt iste
    </p>

        <a href="/newPage">new page</a>

</body>
</html>

Учитывая это, я бы порекомендовал вам посмотреть express stati c files или node-stati c, если вы хотите иметь только stati c сайт

...