“Buat Modul Node Anda Sendiri” Kode Jawaban

Buat Modul Node Anda Sendiri

mycoolmodule/index.js
export.a = "AAAAAA;
exports.hw = function() {
  console.log("Hello World!");
}
/*you can exports as many variables and functions as you want*/

routes/index.js
var express = require('express');

var mymodule = require('mycoolmodule')

var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
	mymodule.hw();
/*	console.log(mymodule.a) will yield AAAAAA*/
});
 
module.exports = router;
Javasper

Buat Modul Node Anda Sendiri

cd node_modules
mkdir myCoolModule
cd myCoolModule
touch index.js
npm init
(answer yes to all)
exports.? all functions and variables you want to be used in other places.
/*for react*/

import {?1, ?2} from 'myCoolModule'
/*for expressJS*/

const myCoolModule = require('myCoolModule');
myCoolModule.?1
myCoolModule.?2
Javasper

Jawaban yang mirip dengan “Buat Modul Node Anda Sendiri”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya