express error - cannot find module - static file

Mike

I keep getting an error that says it cannot find the module reddit.js. I have a folder called "routes" (without quotes) in my directory. In that folder I have reddit.js which is middleware. On the first file below, I did change it to var reddit = require('./routes/reddit.js') and I received the error messsage that says "throw new TypeError('Router.use() requires middleware function but got a ^ TypeError: Router.use() requires middleware function but got a Object at Function.use "

When I keep the code as shown below I get this error:

Error: Cannot find module 'reddit.js'


my app.js file contains the following code:

var express = require('express');
var app = express();
var fs = require('fs');
var reddit = require('reddit.js');

app.use ('/', reddit);
app.use(express.static('public'));
app.use(express.static('public/js'));
app.use(express.static('public/images'));
app.use(express.static('routes'));

my reddit.js file contains the following code:

var express = require ('express');
var request = require ('request');
var reddit = express.Router();

reddit.get(function (req, res, next) {
    request('https://www.reddit.com/r/Showerthoughts/hot.json',function(error, response, body){
        console.log(body);
        var docs = JSON.parse(body).response;
        //var titles = [];
        console.log(docs);
        res.send(docs);
        next;
    });
});

what am I doing wrong?

Swaraj Giri

Mentioned below are the list of things that are not correct

  • You don't need to have .js extensions for including files. Use require('/path/to/reddit'); instead of require('reddit.js');

  • You need to export the router instance in reddit.js. Add module.exports = reddit; at the end of the file.

  • Don't call next() after sending out the response using res.send(docs);

  • Routes are not static content. Remove app.use(express.static('routes'));

  • app.use(express.static('/public')); handles all static content inside the /public folder. You do not need to add app.use(express.static('/public/js'));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Heroku error: 'cannot find module 'express'

From Dev

Webpack + Express + EJS: Error: Cannot find module "."

From Dev

Heroku deploy error : Cannot find module 'Express'

From Dev

Cannot find module express

From Dev

Cannot find module from local file in express js app

From

Node.js Error: Cannot find module express

From Dev

Node.js Express Error: Cannot find module

From Dev

How to fix Node JS error cannot find module 'express' on heroku?

From Dev

Error: Cannot find module 'html' with Node.JS and Express

From Dev

Cannot find module 'express' on cpanel

From Dev

cannot find module file

From Dev

Error: Cannot find module './'

From Dev

express can 't find helper function module Error: Cannot find module './helpers/isPrime'?

From Dev

Express 4.0: Cannot find module 'html' & Cannot find module 'handlebars'

From Dev

Node JS cannot find module error for file in another folder

From Dev

Error "Cannot find module" when importing a remote JSON file in TypeScript

From Dev

Error: Cannot find module './definitions/init' while parsing file

From Dev

Cannot find module, pointed to file

From Dev

Error: Cannot find module 'commander'

From Dev

Error: Cannot find module 'mongodb'

From Javascript

Error: Cannot find module html

From Dev

Error: Cannot find module './args'

From Dev

Phonegap error: Cannot find module

From Dev

NPM - Error: Cannot find module '../'

From Dev

Error: Cannot find module 'libxmljs'

From Dev

Error: Cannot find module './development'

From Dev

Error: Cannot find module './algs'

From Dev

Error: Cannot find module 'ms'

From Dev

Error: Cannot find module './pulp'