7

This is my project structure:

enter image description here

This is index.js.

var express = require('express');
var router = express.Router();
var fs = require('fs');
var links = require('../models/Links');

var readline = require('linebyline');
var rl = readline('../data.txt');
router.get('/', function (req, res) {

    rl.on('line', function (line, lineCount, byteCount) {
        var data = line.split(',');
        var id = data[0];
        var url = data[1];           
    })
});

module.exports = router;

What am I doing wrong?

I tried rewriting

var rl = readline('/../data.txt');
var rl = readline(__dirname +'/../data.txt');

Nothing works.

Shafayat Alam
  • 702
  • 1
  • 14
  • 32

1 Answers1

8

Your readline invocation is still going to be relative to the directory your app is running in (your root, where app.js resides), so I don't think you need the parent directory reference.

It should be just

var rl = readline('./data.txt');

Or if you want to use __dirname

var rl = readline(__dirname + '/data.txt');
Wake
  • 1,686
  • 10
  • 14
  • thanks , figured that. i had to call readline right before rl.on('line') too – Shafayat Alam Aug 10 '16 at 16:06
  • Makes sense, although that's not what was causing the error in question: Error: ENOENT: no such file or directory, though the file exists – Wake Aug 11 '16 at 07:50