自分のusers.jsモジュールをルーターファイルに含めようとしています。それはエラーを投げ続けます:
モジュール './router/users.js'が見つかりません
私のディレクトリ構造は次のとおりです。
nodejs (ドライブのメインフォルダー)
--expressserver.js (マイサーバーファイル)
--package.json
-ルータ(ルータmain.jsとusers.jsを含むフォルダユーザ詳細ファイル)
----- main.js
----- users.js
----- orders.js
ここで、私のユーザーモジュールは私のルーター(main.js)と同じフォルダーにあります
ルーターの私のコードは次のとおりです。
var url = require('url');
var users = require('./router/users.js');
module.exports = function (app) {
app.get('/', function (req, res) {
res.render('index.html');
console.log("Home page displayed");
});
app.get('/login', function (req, res) {
res.render('login.html');
console.log("Log in page displayed");
});
app.get('/api/orders/:id', function (req, res) {
console.log(req.params.id);
res.json(ORDER.orders[req.params.id]);
});
app.get('/api/orders/', function (req, res) {
console.log(ORDER);
res.json(ORDER);
});
app.get('/api/users/:id', function (req, res) {
console.log(req.params.id);
res.json(USERS.users[req.params.id]);
});
app.get('/api/users/', function (req, res) {
console.log(USERS);
res.json(USERS);
});
users.jsの私のコード:
module.exports = function () {
// Create User prototype and objects
var USERS = { users: [] };
function User(type, useremail, password) {
this.type = type;
this.useremail = useremail;
this.password = password;
}
var Bob = new User("rep", "[email protected]", "qwerty");
USERS.users.push(Bob);
var Helen = new User("rep", "[email protected]", "test");
USERS.users.push(Helen);
var Dominic = new User("customer", "[email protected]", "1234");
USERS.users.push(Dominic);
var James = new User("grower", "[email protected]", "pass1");
USERS.users.push(James);
};
私はこれにかなり慣れていませんが、モジュールについてたくさん読んでいますが、それでもそれを理解することはできません。私がどこで間違っているかについての提案はありますか?または、問題を修正するために何をする必要がありますか?
注:以前は、ルーターのmodule.exports = function (app) {
周囲とサーバーを使用して、ルーターをサーバーファイルに含めるために次のように同じことを行いました。require('./router/main')(app);
モジュールへの相対パスを指定したので、そのパスは、require
実行されるソースファイルのディレクトリからの相対パスです。あなたの場合、それはすでに「router」ディレクトリに相対的です。これは機能します:
var users = require('./users.js');
または、拡張子が自動的に解決されるため、次のようになります。
var users = require('./users');
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加