This is my code
const express = require('express');
const app = express();
const bodyparser = require('body-parser');
app.use(bodyparser.urlencoded({extended: false}));
app.use(bodyparser.json());
const cors = require('cors');
app.use(cors());
app.use(express.static('website'));
const port = 8000;
const server = app.listen(port, () => {
console.log('the server is up and running');
console.log(`in a localhost ${port}`);
})
and i linked it to html page which has the code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> Hello World!</title>
<script src='L2Server.js' > </script>
</head>
<body>
<p>Hi!</p>
<script src='L2Server.js' ></script>
</body>
</html>
and when I run the code in the terminal it show that the server is up and running then when I open the localhost in the browser it displays a message with the following "CANNOT GET/" and I tried almost everything to solve this problem but i don't know what is the matter.
CodePudding user response:
You need to define a route like
app.get('/', function(req, res) {
// set response here
});
or provide static content from a folder
app.use(express.static(__dirname '/public'));
In your case place your html code in ./public/index.html
and provide it as static content.
CodePudding user response:
Add something like this:
app.get('/', (req, res) => {
res.send('website');
});
where 'website' is the name of your html