-1

I want to use the same structure express gives (when generating like express appname) and inside views folder i want to use a normal html file. I can do this if I create main js file (app.js) by manually. but what i want to know is how can i change the app.js file which generated when creating a express as i want.

I did this code but its not working.

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

// my HTML file inside the views directory
 app.set('views', path.join(__dirname, 'views'));

router.get('/', function(req, res) {

res.sendFile(__dirname + '/index.html');

});

I am getting this errors

Error: No default engine was specified and no extension was provided. at new View

Samz
  • 76
  • 10

1 Answers1

2

If you want to serve up static files that you write yourself, you can easily use Express's static method to setup a folder to serve up all files within it.

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

app.use('/', express.static(`${__dirname}/views`));
app.listen( ... );

Until you have the need for views, I encourage you to use static files until then to keep things simple.

Rob Brander
  • 3,702
  • 1
  • 20
  • 33
  • I already have this code part app.use(express.static(path.join(__dirname, 'public'))); So this means can i make my html files in this public directory ? – Samz Jun 30 '18 at 05:26
  • This wasn't the complete thing i want. But i manged it with this answer. Thank you. – Samz Jun 30 '18 at 05:50