Answer the question
In order to leave comments, you need to log in
How to include a javascript file in a NodeJS project?
Good afternoon.
I created calc.js with Jjon.js (parser generator) so got a compiler for the simplest calculator language. In order for the parser to work, you need to run the calc_run.js file in the terminal through NodeJS. The code for the example startup file calc_run.js is very simple, two lines. The code works (answer = 3) if you enter in the terminal: node calc_run.js
var parser = require('./calc.js');
console.log(parser.parse('1+2'));
var http = require('http'),
fs = require('fs');
fs.readFile('./calc.html', function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(8000);
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>hello</h1>
<!-- Как подключить скрипт calc_run.js? -->
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Scripts are inserted into HTML through the script
tag
. But it will not be loaded, because your server can only serve one html page. look at the Express
framework
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question