简体   繁体   English

将本地Javascript文件导入NodeJS

[英]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? NodeJS Web应用程序是否可以在不使用任何中间件模块的情况下“导入”并使用本地Javascipt文件的功能?

EDIT: 编辑:

cont.js 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 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 server.js的相对路径为:./cont/cont.js

Yes you can just require('relative filename') and use the code but your cont.cont.js file must add an export 是的,您只需要(“相对文件名”)并使用代码,但是您的cont.cont.js文件必须添加导出

Something like: 就像是:

cont.js 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 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);
           }  

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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