我不明白为什么错误显示“找不到模块”?

时间:2019-11-18 10:48:52

标签: node.js

app.js

var http = require('http');
const module1 = require('./module1');

function onRequest(request,response){
    response.writeHead(200,{'Content-type': 'text/plain'});
    response.write(module1.myStr);
    //module1.myfun();
    response.end();

}
http.createServer(onRequest).listen(8000);

module1.js

function myfun(){
    console.log("fun is called");
}

var myStr = 'String!!'


module.exports.myfun = myfun;
module.exports.myStr = myStr;

academind遵循了nodejs基本教程,并写下了完全相同的代码。

2 个答案:

答案 0 :(得分:-1)

您可以尝试这个。而且两个文件都应该在同一个文件夹中

app.js

var http = require('http');
const module1 = require('./module1');

function onRequest(request,response){
    response.writeHead(200,{'Content-type': 'text/plain'});
    response.write(module1.myStr);
    //module1.myfun();
    response.end();

}
http.createServer(onRequest).listen(8000);

module1.js

function myfun() {
  console.log("fun is called");
}


var myStr = "String!!";

module.exports.myfun = myfun;
module.exports.myStr = myStr;

以下是实时代码:https://codesandbox.io/s/wizardly-leftpad-2jl5w

答案 1 :(得分:-2)

尝试执行此操作:

<?php

namespace App\Http\Controllers;

use NotificationChannels\ChatAPI\ChatAPIChannel;
use NotificationChannels\ChatAPI\ChatAPIMessage;
use Illuminate\Notifications\Notification;

use Utils\Twilio;

class Controller extends Notification
{
    public function via($notifiable)
    {
        return [ChatAPIChannel::class];
    }

    public function toChatAPI()
    {
        $to1 = '+947xxxxxxxx';
        $from1 = '+947xxxxxxxx';

        if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $to1,  $matches ) )
        {
          $to = $matches[1] . '-' .$matches[2] . '-' . $matches[3];            
        }

        if(  preg_match( '/^\+\d(\d{3})(\d{3})(\d{4})$/', $from1,  $matches ) )
        {
          $from = $matches[1] . '-' .$matches[2] . '-' . $matches[3];            
        }

        $body = 'Hello Bro!';
        $twilio = new Twilio;

        try {
            return $twilio->sendSMS($from, $body, $to);
        } catch (\Throwable $th) {
            dd($th);
        }
    }
}

这应该可行。