Module not found error - NodeJS Express.JS

I'm trying to catch the post data from my form and when I'm done with processing I want it to render the index.html file again.

Although when I'm trying the code as displayed below, I get an error.

The error:

Error: Cannot find module 'html'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at new View (/Applications/XAMPP/xamppfiles/htdocs/controlpanel/node_modules/express/lib/view.js:81:14)
    at Function.render (/Applications/XAMPP/xamppfiles/htdocs/controlpanel/node_modules/express/lib/application.js:570:12)
    at ServerResponse.render (/Applications/XAMPP/xamppfiles/htdocs/controlpanel/node_modules/express/lib/response.js:1008:7)
    at /Applications/XAMPP/xamppfiles/htdocs/controlpanel/server.js:14:9
    at Layer.handle [as handle_request] (/Applications/XAMPP/xamppfiles/htdocs/controlpanel/node_modules/express/lib/router/layer.js:95:5)
    at next (/Applications/XAMPP/xamppfiles/htdocs/controlpanel/node_modules/express/lib/router/route.js:137:13)

The code:

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

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

app.post('/', function(req, res, next) {
    console.log('start processing postdata...');
    next()
});

app.all('/', function(req, res) {
    res.render('html/index.html');
});

app.listen(2222);

Everything works fine for the GET method. Only the POST request is causing this error.

What am I doing wrong?

Thanks in advance,
Laurens

Upvotes: 0

Views: 805

Answers (1)

Sanket
Sanket

Reputation: 985

Here is the working code, you should use sendFile instead if render. Render is been used with views.

'use strict';
let express = require('express');
// let session = require('express-session');
let app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use('/public', express.static('public'));
app.use(express.static('public/html'));

app.post('/', function (req, res, next) {
    console.log('start processing post data...');
    next();
});

app.all('/', function (req, res) {
    res.sendFile('./index.html', {
        root: __dirname + '/public/html'
    });
});

app.listen(2222);

Upvotes: 1

Related Questions