如何读取带有节点的文本文件。把它写进HTML页面?

时间:2023-01-09 18:59:35

I have a text file that I would like to read with Node.js using the fs module. I know how to read the file but I do not know how to take the data of the text files and be able to put it to a website page.

我有一个文本文件,我想用Node来读。使用fs模块的js。我知道如何读取文件,但我不知道如何获取文本文件的数据并能够将其放到网站页面。

For Example: I would like to read a file that has the contents of 'hello world!!!' and then using jQuery put it to a div.

例如:我想读一个包含“hello world!!”然后使用jQuery将它放到div中。

4 个解决方案

#1


2  

NodeJS is not a web server.

NodeJS不是web服务器。

However, you can easily add dependencies to provide such capabilities.

但是,您可以很容易地添加依赖项来提供此类功能。

e.g. express, koa, or hapi.

例如express、koa或hapi。

So far, you've got [something like]:

到目前为止,你已经有了一些类似的东西:

var fs = require('fs');
fs.readFile('data.json', (e, data) => {
    if (e) throw e;
    console.log(data);
});

You could use express as follows (note: if you have not already run npm init, do so and provide sensible defaults):

您可以使用express如下(注意:如果您还没有运行npm init,请这样做,并提供合理的默认值):

npm init 
npm install --save express

Then, create a file, app.js, to serve you're data, e.g.:

然后,创建一个文件app.js,为你的数据服务,例如:

var express = require('express');
var app = express();

app.use('/', (req, res) => {
    if (e) throw e;

    // **modify your existing code here**
    fs.readFile('data.json', (e, data) => {
        if (e) throw e;
        res.send(data);
    });
});

app.listen(5555);

Launch your node "web server":

启动节点“web服务器”:

node app.js

Finally, point your browser to:

最后,将浏览器指向:

http://localhost:5555/

#2


0  

I don't know what is the purpose of this, but you can use Express to start a simple web server that provide the content of your text file. Then, you just need to request this web server from your website page using jQuery.

我不知道这样做的目的是什么,但是您可以使用Express启动一个提供文本文件内容的简单web服务器。然后,您只需要使用jQuery从您的网站页面请求这个web服务器。

#3


0  

If you are using jquery and express just build an endpoint on your express server that serves the contents of the text file.

如果您正在使用jquery和express,那么只需在您的express服务器上构建一个端点,该端点服务于文本文件的内容。

your jquery:

jquery:

$.getJSON("/text", function(data){
   <write code here to render contents of text file to the DOM>
})

your end point in node:

节点的端点:

router.get("/text", function(req, res){
      fs.readFile(textFile, 'utf8', function(err, data) {
        if (err) throw err;
        return res.json(textFile);
      })
    })
})

#4


0  

As I understand your question you want to "render" the text on the client, not on the server. The easiest way to do this with jQuery is using $.ajax like this:

我理解您的问题,您希望在客户机上而不是服务器上“呈现”文本。使用jQuery最简单的方法是使用$。ajax是这样的:

const URL_TO_STATIC_TXT = 'https://cdn.rawgit.com/fabe/2a371ce28effb32fa1120f8d25225d37/raw/6d0bfebff1d0b52d72ed5ded4011a0bbff80d679/file.txt';
  
$.ajax({ url: URL_TO_STATIC_TXT })
  .done(data => {
    $('body').text(data);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Then you only need to host the static .txt files with Node.js, without even using fs. With Express you can achieve this with app.use:

然后,您只需将静态的.txt文件与Node一起托管。甚至不使用fs。使用Express,你可以通过应用程序实现这一点。

app.use('/', express.static(__dirname + '/public'));

If you want to render the files on the server (using fs) you can also look into countless templating libraries like pug.

如果您想在服务器上呈现文件(使用fs),您还可以查看无数的模板库,比如pug。

#1


2  

NodeJS is not a web server.

NodeJS不是web服务器。

However, you can easily add dependencies to provide such capabilities.

但是,您可以很容易地添加依赖项来提供此类功能。

e.g. express, koa, or hapi.

例如express、koa或hapi。

So far, you've got [something like]:

到目前为止,你已经有了一些类似的东西:

var fs = require('fs');
fs.readFile('data.json', (e, data) => {
    if (e) throw e;
    console.log(data);
});

You could use express as follows (note: if you have not already run npm init, do so and provide sensible defaults):

您可以使用express如下(注意:如果您还没有运行npm init,请这样做,并提供合理的默认值):

npm init 
npm install --save express

Then, create a file, app.js, to serve you're data, e.g.:

然后,创建一个文件app.js,为你的数据服务,例如:

var express = require('express');
var app = express();

app.use('/', (req, res) => {
    if (e) throw e;

    // **modify your existing code here**
    fs.readFile('data.json', (e, data) => {
        if (e) throw e;
        res.send(data);
    });
});

app.listen(5555);

Launch your node "web server":

启动节点“web服务器”:

node app.js

Finally, point your browser to:

最后,将浏览器指向:

http://localhost:5555/

#2


0  

I don't know what is the purpose of this, but you can use Express to start a simple web server that provide the content of your text file. Then, you just need to request this web server from your website page using jQuery.

我不知道这样做的目的是什么,但是您可以使用Express启动一个提供文本文件内容的简单web服务器。然后,您只需要使用jQuery从您的网站页面请求这个web服务器。

#3


0  

If you are using jquery and express just build an endpoint on your express server that serves the contents of the text file.

如果您正在使用jquery和express,那么只需在您的express服务器上构建一个端点,该端点服务于文本文件的内容。

your jquery:

jquery:

$.getJSON("/text", function(data){
   <write code here to render contents of text file to the DOM>
})

your end point in node:

节点的端点:

router.get("/text", function(req, res){
      fs.readFile(textFile, 'utf8', function(err, data) {
        if (err) throw err;
        return res.json(textFile);
      })
    })
})

#4


0  

As I understand your question you want to "render" the text on the client, not on the server. The easiest way to do this with jQuery is using $.ajax like this:

我理解您的问题,您希望在客户机上而不是服务器上“呈现”文本。使用jQuery最简单的方法是使用$。ajax是这样的:

const URL_TO_STATIC_TXT = 'https://cdn.rawgit.com/fabe/2a371ce28effb32fa1120f8d25225d37/raw/6d0bfebff1d0b52d72ed5ded4011a0bbff80d679/file.txt';
  
$.ajax({ url: URL_TO_STATIC_TXT })
  .done(data => {
    $('body').text(data);
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Then you only need to host the static .txt files with Node.js, without even using fs. With Express you can achieve this with app.use:

然后,您只需将静态的.txt文件与Node一起托管。甚至不使用fs。使用Express,你可以通过应用程序实现这一点。

app.use('/', express.static(__dirname + '/public'));

If you want to render the files on the server (using fs) you can also look into countless templating libraries like pug.

如果您想在服务器上呈现文件(使用fs),您还可以查看无数的模板库,比如pug。