i want to load a JS file into my EJS File but it doesnt find the JS File.
I use "app.use('/', express.static(path.join(__dirname, '/frontend')));" but it doesnt work.
Or is there a better way instead of include a js file to ejs file.
..............................
Folder Structur:
│
├──server.js
├──package-lock.json
├──package.json
│
├── js/
├── data.js
├── node_modules/
└── views/
├── index.ejs
└── 404.ejs
server.js
'use strict';
const http = require('http');
const express = require('express');
const path = require('path');
const app = express();
app.use('/', express.static(path.join(__dirname, '/frontend')));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
const blogs = [
{ titel: 'Neuer Titel', desc: 'Data'},
{ titel: 'Titel 2', desc: 'New Desc'}
];
res.render('index', {
title: 'Home',
blogs
});
});
app.use('/*', (req, res) => {
res.status(404).render('404');
});
app.listen(process.env.PORT || 3000, () => {
console.log('Server listen');
});
Index.ejs
<html lang="en">
<head>
<meta charset="utf-8">
<title>Data | <%= title %></title>
</head>
<body>
<nav>
<ul>
<li>
<a href="/">Data</a>
</li>
<li>
<a href="/about">Data2</a>
</li>
<li>
<a href="/about/create">Data3</a>
</li>
</ul>
</nav>
</body>
<script src="/frontend/data.js"></script>
</html>
/frontend/data.jsin your browser? I'm guessing/data.jswould work. - thgaskell