user9384488
user9384488

Reputation:

How to serve HTML+JQuery Web Application with NodeJs+Express?

I have my app.js default configuration created using express generator. my view has .jade files but I want to serve plain HTML files with jquery included in my script tag. The content of these HTML files is not static but will be fetched via ajax calls so the pages are not strictly static.

I have read other answers but have not found a variation of Jquery+HTML files served from a node backend.

Can somebody please let me know how to achieve this?

EDIT

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

Upvotes: 1

Views: 265

Answers (1)

Ameya
Ameya

Reputation: 61

Try using :

app.use(express.static(path.join(__dirname, '/path/to/your/index.html')));

_dirname parameter takes in a path that directly contains your index.html file. So you either have to make sure that your public folder has index.html file in it or you have to pass the path to your specific folder.

To answer your jquery question, you can include it in your index.html. Because that is the file from where your frontend bootstraps.

Upvotes: 2

Related Questions