Hi I’m trying to create a database to connect to nodejs however it shows me CANNOTGET/ when I load server
Is it because I need to add function?
This is my code below
const mysql = require('mysql');
const express = require('express');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '1234',
database: 'certdb'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
});
const port = 3000;
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const ip = require("ip");
const app = express();
const server = http.createServer(app);
const ws = new WebSocket.Server({ server: server });
app.use( express.static(`${__dirname }/webpages`) );
// start the server
server.listen(port, () => {
console.log('Server started:', `http://${ip.address()}:${port}` )
});
Please format your code with backticks like below:
```
// your code here
```
Result:
// your code here
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Note: Backticks are not single quotes.

1 Like
You do not have a route for / with GET request.
But the express.static
finds files from a directory.
express.static
is just a middleware that our app
uses to load resources, such as html
files. A route (endpoint) is a specific location on our app
that our users can visit via various HTTP methods, such as GET
and POST
. In this case, we haven’t created the main (root) route of our app
, that is /
, via GET
request. Please, see the hello world
of a Node/Express app here https://expressjs.com/en/starter/hello-world.html.
2 Likes
Ok. But if you went to localhost/index.html
and index.html
was in webpages
, would it show correctly? (yeah, just / might not work.)
yes, know most of that. But with the code at the top, would /index.html
render correctly if it was in webpages
?
static
lets the server know where the files are. it doesnt do anything with them. as long as you use static() correctly for the folder you need then it will work
/index.html
would only work if that is what you set your route at when you wrote your GET request and if you used res.render('index')
for that route. usually for your index you just set it to /
eg
app.get("/index.html", (req, res) => {
res.render("index")
})
also you gotta make sure you include the port number in your localhost address
eg. localhost:3000/index.html
1 Like