0

I have some data that I want to store locally and to be able to pull it dynamically, maybe in another session or after the browser was closed and all browser data was cleared.

I run the site with http-server CLI command and navigate to localhost to access it from the browser.

How can I send data to the server side so the server side will save the data as a file?

I tried to do an ajax post request to see if something happens in the console, but it just returned 404 and nothing came up in the console.

The docs don't mention anything about post requests: https://www.npmjs.com/package/http-server

PS: I have to run this with http-server, this is an offline project.

shinzou
  • 5,850
  • 10
  • 60
  • 124
  • You need a database (maybe some noSQL package could do, I'm not familiar enough to provide specific details) – Alex Mar 02 '17 at 13:06
  • Does your httpserver have a function to accept the post request and write the file yet? If not, you'll have to write it before you can actually accept the POST, else the server doesn't know what to do with it. – Shilly Mar 02 '17 at 13:09
  • @Alex Can I do it without a database? Just to send a string and store it in a file? – shinzou Mar 02 '17 at 13:09
  • @Shilly where do I put this code? edit the code in node_modules of http-server module? – shinzou Mar 02 '17 at 13:10
  • But for the record, http-server is usually used to serve only static files. It's probably going to be easier to use the default http module and write 20 lines to accept the post request. – Shilly Mar 02 '17 at 13:11
  • Trying to find it in the docs, but so far, my conclusion is that the http-server module can't actually do any custom scripting. If that is true, the moduel you use isn't fit to solve your problem. – Shilly Mar 02 '17 at 13:12
  • @Shilly what module would you recommend to use to replace http-server? – shinzou Mar 02 '17 at 13:13
  • Basic node http module, combined with fs module and write a few lines of JS to accept the POST with the http module and write the file with the fs module. Unless ofcoruse you want to go full out and use one of the excellent tools rsp suggests below. Although they might have a higher learning curve. – Shilly Mar 02 '17 at 13:15

2 Answers2

4

You will not be able to do this with http-server alone, because http-server can only serve static content and cannot be used to run any code on the server side.

You will have to write a backend yourself, possibly using a framework like Express, Hapi, Restify, Loopback etc. and serve your static files that you need with your new backend, or keep it served as you do now but then you will probably need to take CORS into account if you use different ports for your data saving/retrieving endpoints and your static content - unless you run a reverse proxy that makes it all appear on the same host name and port.

You can use the file system to save the data or you can use a database - either a standalone database like Mongo or Postgres or an embedded database like SQLite or Loki.

For examples on how to serve static content in your own backend see:

Community
  • 1
  • 1
rsp
  • 107,747
  • 29
  • 201
  • 177
1

You should use express for this kind of stuff. You can easily make methods that handle certain requests.

Here is an exmaple on how to handle a get request by just sending some data

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

app.get('/', function (req, res) {
  res.send('Hello World')
})

app.listen(3000)

And you can use the fs api from node itself to write data.

var fs = require('fs')
fs.writeFile('message.txt', 'Hello Node.js', (err) => {
  if (err) throw err;
  console.log('It\'s saved!');
});

Note: the fs example uses arrow functions. You can find more information here

Murf
  • 1,661
  • 11
  • 18