I am new in computer science and i love programming. So I start learning nodejs and i have wrote this simple code in test.js..
var http = require("http");
var fs=require('fs');
var path=require('path');
var msg= "hi\nhow r u ?\nfine";
var file='textms.txt';
http.createServer(function (req,res){
fs.open(file,function(exists){
if(exists){
fs.open(file,msg,function(err){
if(err)throw err;
});
}else{
fs.writeFile('test.txt',msg);
console.log('New file is created : ');
}
});
res.end();
}).listen(8080);
console.log('server running on port 8080');
this is my index.html page code ..
<html>
<head>
<title>
Chat Test With Nodejs
</title>
<script type="text/javascript" src="test.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<div id="cnt"></div>
<input type="text" class="text">
<button id="btn">Send</button>
</body>
</html>
i run this code on chrome(localhost:8080). it works fine.
Problem: when i run my index.html page on chrome, i get this error in chrome console:
Uncaught ReferenceError: require is not defined @ server.js:1
i tried to search on google, i didnt find anything useful. Questions: why i am getting this error ?
runor create a server in NodeJS andServeyour HTML files through it.