I have a node server for a webpage with the following folder structure:
server.js
public/
|_ index.html
|_ main.js
|_ pages_content/
|_contents.js
server.js
serves content in public/
:
var express = require('express');
var app = express();
var path = require('path');
var public = path.join(__dirname, 'public');
app.get('/', function(req, res) {
res.sendFile(path.join(public, 'index.html'));
});
app.use('/', express.static(public));
app.listen(8080);
html.js
defines a navbar, whose items have a function for switching current active navItem:
<body>
<nav id = "navbar">
<div id = "projects" onClick="renderContent(this)" class = "navItem activeNavItem">PROJECTS</div>
<div id = "experiences" onClick="renderContent(this)" class = "navItem">EXPERIENCE</div>
<div id = "resume" onClick="renderContent(this)" class = "navItem">RESUME</div>
</nav>
<script type="module" src="/pages_content/contents.js"></script>
<script type="module" src="main.js"></script>
</body>
main.js
defines the renderContent()
function that will activate current nav item and lately insert content of correspondent nav item into body:
import {PROJECTS_CONTENT} from '/pages_content/contents.js'
const renderContent = function(navItem){
contentOptionId = navItem.id
activateNavbar(contentOptionId)
console.log(PROJECTS_CONTENT)
}
const activateNavbar = function(optionId){
NAVBAR_SELECTOR = "#navbar"
ACTIVE_ITEM_CLASS = "activeNavItem"
const navBar = document.querySelector(NAVBAR_SELECTOR)
for (navItem of navBar.children){
if(navItem.id === optionId){
navItem.classList.add(ACTIVE_ITEM_CLASS)
} else {
navItem.classList.remove(ACTIVE_ITEM_CLASS)
}
}
}
Finally, /page_content/contents.js
holds an object with the contents to be rendered:
const PROJECTS_CONTENT = [
{
"title": "Certificate Generator",
"firstParagraph": "Node.js batch script that parses a .csv file and generates .pdf certificates for each line/person",
"firstImgUrl": "images/certificate0.png",
"secondImgUrl": "images/certificate.png",
"projectUrl": "https://github.com/USPCodeLabSanca/Node-Batch-Certificate-Generator",
"secondParagraph": "Mentor of the project, responsible for idealizing, writing requirements, spliting requirements in features divided across team and coding",
"thirdParagraph": "Used in real life recurrently for generating USPCodelab courses, hackathons and other events certificates."
}
]
export {PROJECTS_CONTENT}
I'm having issues importing PROJECTS_CONTENT
variable from a secondary file. I made both scripts a module
, exported PROJECTS_CONTENT
on its file and imported it at main.js
.
This seems to have made PROJECTS_CONTENT
visible at main.js
because no more error are being thrown at console.
However, the function renderContent()
, that used to work before messing up with modules, now throws an error:
Uncaught ReferenceError: renderContent is not defined
onclick http://localhost:8080/:1
I've tried multiple things, such as export
ing renderContent
as another stackoverflow question suggested:
import {PROJECTS_CONTENT} from '/pages_content/contents.js'
export const renderContent = function(navItem){
contentOptionId = navItem.id
activateNavbar(contentOptionId)
console.log(PROJECTS_CONTENT)
}
export const activateNavbar = function(optionId){
NAVBAR_SELECTOR = "#navbar"
ACTIVE_ITEM_CLASS = "activeNavItem"
const navBar = document.querySelector(NAVBAR_SELECTOR)
for (navItem of navBar.children){
if(navItem.id === optionId){
navItem.classList.add(ACTIVE_ITEM_CLASS)
} else {
navItem.classList.remove(ACTIVE_ITEM_CLASS)
}
}
}
But none of them seem to work. How can this be fixed? Thanks a lot in advance