简体   繁体   中英

Error :Enoent :no such file or directory ,stat 'D:\awesome-testindex.html'

I have a folder named awesome test and it contains index.hml ,node modules and server.js .Here is the server.js file and i am getting this error .

//grab express
var express=require('express');
//create an express App
var app=express();
// create an express route for the home page
// http://localhost:8080/
app.get('/', function(req, res) {
res.sendFile(__dirname + 'index.html');
});
// start the server on port 8080
app.listen(8080);
// send a message
console.log('Server has started!');

Here's where the error is: res.sendFile(__dirname + 'index.html');

It should be: res.sendFile(__dirname + '/index.html');

The reason for this is because the index.html is being added upon the directory, which doesn't end with a / by default. You need to add it yourself as shown above. Hope this helps!

Edit : I tried Node.js before. I think it would be best if you added a "public" folder, with the .js file being above everything. Here's an example:

Node.js目录示例

This was my code for my first Node.js server, as a reference:

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;
const express = require('express');
const app = new express();
app.use(express.static(__dirname + '/public'));
app.listen(3000, () => console.log("Example app listening on port 3000!"))
console.log("http://"+hostname+":"+port)

Note : To use express as shown above, you'll have to open a command line, and type in the following (assuming you have node.js installed correctly):

npm install -g express

Also, to make sure you installed both node.js, do the following:

Node: node -v

Hope everything helps! ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM