About start a working express server

Hi everybody, i’m studying this challenge, here’s my actual code in myApp.js:

var express = require('express');
var app = express();
console.log("Hello World");
app.get("/", function(req, res) {
  res.send('Hello Express');
}
)

app.get("/", function(req, res) {
  var absolutePath = __dirname + views/index.html;
  res.sendFile(absolutePath);
});


I don’t know where i’m wrong, hope you can help me!

You have two routes for app.get("/",...?

Do you have this in an online IDE somewhere?

Please post all the code from the myApp.js file. If you have a Replit with the code post a link to that.

Did you remove the module.exports = app; at the bottom?

I’ve it on replit here. I’ve edited my code including only one app.get("/",... and 2 functions, but it doesn’t work i think it is syntactically wrong somewhere.

I remember there wasn’t any of this line of code.

This is the starting code.

https://github.com/freeCodeCamp/boilerplate-express/blob/gomix/myApp.js

Look at the bottom of the file.

You have this code:

app.get("/", function(req, res) {
  res.send('Hello Express');

 
  res.sendFile(__dirname + views/index.html);
});

The res.send and res.sendFile are for two different challenges.

Both challenges use the / path but they are for different challenges. Remove the res.sendFile(__dirname + views/index.html); from the code for the challenge you have posted about.

ok, i’ve insert it, but it doesn’t work the same, here’s my actual code:

var express = require('express');
var app = express();
console.log("Hello World");
app.get("/", function(req, res) {
  res.send('Hello Express');

 
  res.sendFile(__dirname + views/index.html);
});


 module.exports = app;


don’t know why…hope you can give me a hint

Read the post I made above.

wrong link to the challenge, this is.
For solving it i wrote this following your message:

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

app.get("/", function(req, res) {
    res.sendFile(__dirname + views/index.html);
});


 module.exports = app;

but it still doesn’t work.

You should see views is not defined or something.

  1. The path is a string which is why you do string concatenation.

Example:

res.sendFile(pathVariable + pathLiteralString)

  1. The views folder path should be relative so / in front of the path.

A bit more spoilery help

res.sendFile(__dirname + '/someFolder/someFile.html')

1 Like

I forgot the quotation marks! that was my problem :joy: thank you!

this post helped me a bit, I was also copying the address literally :sweat_smile:

1 Like