Friday, 28 April 2017

node js GET 404 (Not Found)

I am new to node.js. i am running nodejs from bin/wwww and getting "jquery.min.js:4 GET http://127.0.0.1:3000/file.js 404 (Not Found)" error. I am trying very basic and simple thing in a simple web page. Just trying to execute a file on server through ajax call but it always shows file not found error.

In my html page (firstpage.html) i am trying which i want as client request.

<!DOCTYPE html>
<html>

<head>
<meta content="en-us" http-equiv="Content-Language">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<link rel="stylesheet" type="text/css" href="stylesheets/myButton.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>  

<script>
var dataString;

$(document).ready(function(){
    $.ajax({
    type: "GET",
    url: 'file.js',
    data: dataString,
    success: function(data){
       console.log(data);
        }
    });
}); 
</script>
</head>

<body>
<p class="auto-style1"><strong>Welcome to MyPage</strong></p>
<div >
<input class="myButton" type="button" onclick="readTextFile(test.txt)" value="Submit" />
</div>
<button id="bb">QUERY</button>
</body>
</html>

and i have a file (file.js) at server ,

var fs = require("fs");

console.log("Going to write into existing file");
fs.writeFile('input.txt', 'Simply Easy Learning!',  function(err) {
   if (err) {
      return console.error(err);
   }

   console.log("Data written successfully!");
   console.log("Let's read newly written data");
   fs.readFile('input.txt', function (err, data) {
      if (err) {
         return console.error(err);
      }
      console.log("Asynchronous read: " + data.toString());
   });
});

Currently file.js and index.txt are in myapp\public\javascripts location. my app.js is as below,

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');
var firstpage = require('./routes/firstpage');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);
app.use('/firstpage', firstpage);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

Am i making some mistake in configuration ? please help.



via sand

No comments:

Post a Comment