简体   繁体   中英

Import Local Javascript Files into NodeJS

Is it possible for a NodeJS Web Application to "import" and use a local Javascipt File's functions without using any middle ware modules?

EDIT:

cont.js

function getStyles(res,reqFile){
    var options={
                root:__dirname+'/Views/styles/',
                headers:{
                    'Content-Type':'text/css'
                }
            };

                res.sendFile(reqFile,options,function(err){
                    if(err){
                        console.log(err);
                        res.status(err.status).end();
                    }
                    else {
                        console.log("Sent "+ reqFile);
                    }
                });    
}

server.js

var fs = require('fs')
var path = require('path')
var express = require('express')
var app = express();
var url = require('url')
var views="/Views/"

app.get(/\/Views\/styles\//,function(req,res){
var reqPath = url.parse(req.url).pathname;
var reqFile = path.basename(reqPath); // the requested file
console.log("VIEWS/STYLES : " + reqPath);

fs.readdir(__dirname+views+'/styles','utf8',function(err,data){
    console.log(data);
    if(data.includes(reqFile)){
        console.log(reqFile+ " Found in data array" );
          //call function here
          getStyles(res,reqFile);
           }  

});

The relative path to server.js is : ./cont/cont.js

Yes you can just require('relative filename') and use the code but your cont.cont.js file must add an export

Something like:

cont.js

function getStyles(res,reqFile){
    var options={
                root:__dirname+'/Views/styles/',
                headers:{
                    'Content-Type':'text/css'
                }
            };

                res.sendFile(reqFile,options,function(err){
                    if(err){
                        console.log(err);
                        res.status(err.status).end();
                    }
                    else {
                        console.log("Sent "+ reqFile);
                    }
                });    
}

module.exports = getStyles;

server.js

var fs = require('fs')
var path = require('path')
var express = require('express')
var app = express();
var url = require('url')
var views="/Views/"

var getStyles = require('./cont/cont.js')

app.get(/\/Views\/styles\//,function(req,res){
var reqPath = url.parse(req.url).pathname;
var reqFile = path.basename(reqPath); // the requested file
console.log("VIEWS/STYLES : " + reqPath);

fs.readdir(__dirname+views+'/styles','utf8',function(err,data){
    console.log(data);
    if(data.includes(reqFile)){
        console.log(reqFile+ " Found in data array" );
          //call function here
          getStyles(res,reqFile);
           }  

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM