Skip to content

Instantly share code, notes, and snippets.

@pankaj-gecko
Created December 4, 2021 22:02
Show Gist options
  • Save pankaj-gecko/cbed3b716779a82b91325f7e7d55826d to your computer and use it in GitHub Desktop.
Save pankaj-gecko/cbed3b716779a82b91325f7e7d55826d to your computer and use it in GitHub Desktop.
import Express from 'express';
import { URL } from 'url';
import Hbs from 'hbs';
//This code does not works for me since I am using 'Express' function directly.
const __filename = new URL('',import.meta.url).pathname;
const publicDirectoryPath = new URL('../public', import.meta.url).pathname;
const templateDirectoryPath = new URL('../templates/views', import.meta.url).pathname;
Express().use(Express.static(publicDirectoryPath));
Express().set('view engine', 'hbs');
Express().set('views', templateDirectoryPath);
//Main code starts here
Express().get('/', (req,res) => {
res.send("<h1>Hello World</h1>");
});
Express().listen(3000, () => {
console.log("Listening on Port 3000");
});
import Express from 'express';
import { URL } from 'url';
import Hbs from 'hbs';
const __filename = new URL('',import.meta.url).pathname;
//This function works fine as expected
const publicDirectoryPath = new URL('../public', import.meta.url).pathname;
const templateDirectoryPath = new URL('../templates/views', import.meta.url).pathname;
const app = Express();
app.use(Express.static(publicDirectoryPath));
app.set('view engine', 'hbs');
app.set('views', templateDirectoryPath);
//Main code starts here.
app.get('/', (req,res) => {
res.send("<h1>Hello World</h1>");
});
app.listen(3000, () => {
console.log("Listening on Port 3000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment